Search code examples
.netasp.netgridviewobjectdatasource

Gridview and objectDatasource


I am trying to bind this to a gridview..

 var source = from p in allComments
                 select new { p.Img, p.Name, p.Comment };
    GridView1.DataSource = source;
    GridView1.DataBind();

all Comments has But it wont bind cause my gridview allows paging. I read a bit about that problem and i found to bind the gridview ,, i could use ObjectDataSource... And return a dataset to bind to the gridview..

I have got this mark up of the gridview (only part of the mark up):

      <asp:TemplateField HeaderText="#">
                    <HeaderStyle Width="500px" />
                     <ItemStyle Width="500px" Height="100px" />
                    <ItemTemplate>
                        <asp:Label ID="lblMessage" runat="server"  Text='<%# Bind("Comment") %>'></asp:Label>
                    </ItemTemplate>
   </asp:TemplateField>

       <asp:TemplateField HeaderText="#">
                    <HeaderStyle Width="100px" />
                      <ItemStyle Width="100px" Height="100px" />
                    <ItemTemplate>
                        <asp:Image ID="imgName" runat="server"  imageUrl='<%# Bind("Img") %>'></asp:Image><br />
                        <asp:Hyperlink ID="hyperLink" runat="server"  Text='<%# Bind("Name") %>' ></asp:Hyperlink>
                    </ItemTemplate>
   </asp:TemplateField>

What do you think..Should I use objectdataSource?


Solution

  • If you use ObjectDataSource, then Paging and Sorting will automatically work. I would recommend you to use the ObjectDataSource as it has many other features that you can use, like caching etc... Please check this link Displaying Data With the ObjectDataSource

    Check out this for more about ObjectDataSource http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.objectdatasource.aspx