I have a list of objects, Each object has 3 properties..i want to iterate through the list and put them inside a gridview.
public void PutAllObjects in a 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 I should get is my GridView1 having 40 rows. Each row has got 2 columns..the first column has got an image with a hyperlink, and the second column has got the users comments(label)..
Am i right in hte way i write code? or is there a better way to achieve what i want
You should rather do something like this.
If you want to only bind two columns you could do the following.
As you can see above the Image and Hyperlink are both within ItemTempate, which means they will be shown within one cell within the gridview.