Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
HI EXPERTS


I had a datatable which contains 3rows and 3 columns. I want this to be bind in a lable into the webform. Lable to be populate Dynamically. Kindly guide me.

Below is the code i used to do. But it is not working for me.

TableRow tr = new TableRow();
tr.ID = "tr" + count;
tc.ID = "tc" + tablecellid;
lbl.ID = "ID" + tablecellid;
lbl.Text = dtfinal.Rows[i]["TEXT"].ToString();
tablecellid = tablecellid + 1;
tc.ID = "tc" + tablecellid;
lbl.ID = "ID" + tablecellid;
lbl.Text = dtfinal.Rows[i]["DESC"].ToString();
count = count + 1;
tablecellid = tablecellid + 1;
tc.Controls.Add(lbl);
tr.Cells.Add(tc);
Table1.Rows.Add(tr);


Rgds
Jagadesh
Posted
Comments
CodeBlack 26-Aug-13 4:09am    
what error you are getting ? and can you please post your whole code ?
Kumar from madras 26-Aug-13 5:36am    
HI
I am not getting any error. Last Column value only displaying for me.

Rgds
Jagadesh
VICK 26-Aug-13 4:42am    
labelID.text = "what you want to assign";

You can use the above way to populate the label...

If you want to populate the label dynamically with some value of datatable's Row than get the value of specific cell first and than assign it to label.
Kumar from madras 26-Aug-13 5:37am    
Hi khan

I want to assign my datable value.

Rgds
Jagadesh

VICK 26-Aug-13 5:38am    
So HOw are you getting Database values in application?????

Through DataTables or DataSets???

C#
DataTable dt // Your DataTable..

foreach(DataRow dr in dt.Rows)
{
TableRow tr = new TableRow();
tr.ID = "tr" + count;
tc.ID = "tc" + tablecellid;
lbl.ID = "ID" + tablecellid;
lbl.Text = dr["TEXT"].ToString();
tablecellid = tablecellid + 1;
tc.ID = "tc" + tablecellid;
lbl.ID = "ID" + tablecellid;
lbl.Text = dr["DESC"].ToString();
count = count + 1;
tablecellid = tablecellid + 1;
tc.Controls.Add(lbl);
tr.Cells.Add(tc);
Table1.Rows.Add(tr);
}
 
Share this answer
 
HI ALL

I added my control for each row.

Pls find the below code. This solved my problem

TableRow tr = new TableRow();
TableCell tc = new TableCell();
Label lbl = new Label();
tr.ID = "tr" + count;
tc.ID = "tc" + tablecellid;
lbl.ID = "ID" + tablecellid;
lbl.Text = dr["TEXT"].ToString();
tablecellid = tablecellid + 1;
Label lbl2 = new Label();
tc.ID = "tc" + tablecellid;
lbl2.ID = "ID" + tablecellid;
lbl2.Text = dr["DESC"].ToString();
Label lbl3 = new Label();
tc.ID = "tc" + tablecellid;
lbl3.ID = "ID" + tablecellid;
lbl3.Text = dr["STATUS"].ToString();
count = count + 1;
tablecellid = tablecellid + 1;
tc.Controls.Add(lbl);
tc.Controls.Add(lbl2);
tc.Controls.Add(lbl3);
tr.Cells.Add(tc);
Table1.Rows.Add(tr);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900