c# - Putting controls into a gridview Dynamically -


i have list of objects, each object has 3 properties..i want iterate through list , put them inside gridview.

public void putallobjects in gridview(list<commentsobject>commentsdata) {     int num=0;      foreach(var item in commentsdata)      {               image img=new image();               img.imageurl=item.imageofuser;               hyperlink hl=new hyperlink();               hl.text="<br/>"+item.usersname;               gridview1.rows[num++].cells[0].controls.add(img);               gridview1.rows[num].cells[0].controls.add(hl);               lbl=new label();               lbl.text=item.userscomment               gridview1.rows[num].cells[1].controls.add(lbl);       }  } 

what should gridview1 having 40 rows. each row has got 2 columns..the first column has got image hyperlink, , second column has got users comments(label)..

am right in hte way write code? or there better way achieve want

you should rather this.

gridview1.datasource = commentsdata; gridview1.databind(); 

if want bind 2 columns following.

var source = p in commentsdata              select new {p.imageofuser, "<br/>" + p.usersname, p.userscomment }; gridview1.datasource = source; gridview1.databind();    <asp:gridview id="gvview" runat="server" autogeneratecolumns="false">                 <columns>                     <asp:templatefield headertext="#">                         <itemtemplate>                             <asp:image id="imgname" imageurl='<%# bind("imageofuser") %>'></asp:image>                             <asp:hyperlink id="hyperlink" text='<%# bind("userscomment") %>' ></asp:hyperlink>                         </itemtemplate>                         <itemtemplate>                             <asp:label id="lblmessage" text='<%# bind("userscomment") %>'></asp:label>                         </itemtemplate>                     </asp:templatefield>                  </columns>    </asp:gridview> 

as can see above image , hyperlink both within itemtempate, means shown within 1 cell within gridview.


Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -