Each label has id Label1, Label2, and so on.. Now I need to display text of each label as:
For Label1 - first row (second column) value as Label1.Text,
Label2 - second row (second column value) as Label2.Text and so on from DataTable which has two columns (ID, textData) programmatically.
protected void Page_Load(object sender, EventArgs e)
{
DataTable table="Your DataTable";
DataRow[] dr = table.Select("ID>=1"); //Presuming the minimal ID=1,This will select all rows in your DataTable.
int i = 1;
foreach (GridViewRow row in GridView1.Rows)
{
((Label)row.FindControl("Label"+i)).Text = dr[i-1]["textData"].ToString();
i++;
}
}
Please mark the replies as answers if they help or unmark if not.
Feedback to us
Frank Jiang ...
All-Star
16006 Points
1728 Posts
Microsoft
Re: How to bind data to labels programmatically
May 04, 2012 07:26 AM|LINK
protected void Page_Load(object sender, EventArgs e) { DataTable table="Your DataTable"; DataRow[] dr = table.Select("ID>=1"); //Presuming the minimal ID=1,This will select all rows in your DataTable. int i = 1; foreach (GridViewRow row in GridView1.Rows) { ((Label)row.FindControl("Label"+i)).Text = dr[i-1]["textData"].ToString(); i++; } }Feedback to us
Develop and promote your apps in Windows Store