Click here to Skip to main content
15,881,870 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
How to create table dynamically in codebehind and table data come from database. and How to give colspan and rowspan for that table(if one column data is same in 2 rows then i want merge)


Thanks,
Posted
Updated 12-May-20 20:41pm
v3

Hi Friend...

Please look into the below codes.
Table tb = new Table();

tb.BorderWidth = Unit.Pixel(5);

for (int i = 1; i <= Convert.ToInt32(TextBox1.Text ); i++)

{

TableRow tr = new TableRow();

for (int j = 1; j <= 3; j++)

{

TableCell td = new TableCell();

TextBox c_text = new TextBox();

c_text.Visible = true;

c_text.Text = "R"+i.ToString()+j.ToString();

c_text.ID = "R" + i.ToString() + j.ToString();

td.Controls.Add(c_text);

tr.Cells.Add(td);

tb.Rows.Add(tr);

form1.Controls.Add(tb);

}

}



It may be help for u ...
 
Share this answer
 
Comments
Member 10738738 27-Oct-14 3:29am    
foreach (DataRow row in table.Rows)
{

Response.Write("<p>" + row["lastname"] + "" + row["fathername"] + "---" + row["monthername"] + "----" + row["sex"] + "---" + row["dist"] + "--" + row["taluka"] + "----" + row["gram"] + "-----" + row["dob"] + "---" + row["bpalace"] + "----" + row["mobno"] + "---" + row["photo"] + "---" + row["id"] + "</p1>");
}
my problem:- i execute above statement but it displaying outside content place holder
i want this sol:-i want to display row in table format and it should display within content place holder
Hi

Here I'm posting some sample code for that check it once

C#
 string tablestring="";
//dt is datatable object which holds DB results.
 tablestring=tablestring+"<table width="100%"><tr><td>First Column Heading</td><td>Second column Heading</td><td>Third column Heading</td></tr>";
 foreach(datarow dr in dt.rows)
{
   tablestring=tablestring+"<tr><td>"+dr[0].tostring()+"</td><td>"+dr[1].tostring()+"</td><td>"+dr[2].tostring()+"</td></tr>";
}
tablestring=tablestring+"</table>";

divTable.innerHTML=tablestring;


I hope you understood what I did.

All the Best
 
Share this answer
 
Comments
sampath1750 26-Apr-12 7:19am    
Thanks Muralikrishna i have one more problenm.
How to give colspan and rowspan for that table(if one column data is same in 2 rows then i will merge)
Muralikrishna8811 26-Apr-12 7:24am    
same as how you set in html
like
tablestring=tablestring+"<tr><td>"+dr[0].tostring()+"</td><td>"+dr[1].tostring()+"</td><td colspan='2'>"+dr[2].tostring()+"</td></tr>";
Muralikrishna8811 26-Apr-12 7:37am    
To set rowspan you need to do something like
in select query "select B1.title,B1.author,B1.prize,(select count(*) from Books as B2 where B2.title=B1.title) as rspn from Books as B1 group by B1.title

by using that query we can get data with count of title in rspn
thn

string pretitle="";
foreach(datarow dr in dt.rows)
{
if(pretitle=="")
{
tablestring=tablestring+"<tr><td rowspan='"+dr["rspn"].tostring()+"'>"+dr["title"].tostring()+"</td><td>"+dr["author"].tostring()+"</td><td>"+dr["price"].tostring()+"</td></tr>";
pretitle=dr["title"].tostring();
}
else if(pretitle==dr["title"].tostring())
{
tablestring=tablestring+"<tr><td>"+dr["author"].tostring()+"</td><td>"+dr["price"].tostring()+"</td></tr>";
}
else
{
tablestring=tablestring+"<tr><td rowspan='"+dr["rspn"].tostring()+"'>"+dr["title"].tostring()+"</td><td>"+dr["author"].tostring()+"</td><td>"+dr["price"].tostring()+"</td></tr>";
pretitle=dr["title"].tostring();
}
}


you can set like this

this is just idea only you can modify it to achieve your requirement

All the Best
sampath1750 26-Apr-12 7:50am    
Thanks Murali
Muralikrishna8811 26-Apr-12 7:52am    
very welcome
If you are talking about creating a HTML table, then you would use the HtmlTextWriter class to write out HTML. A better bet, though, would be to use a standard DataGrid which allows you to write out the content as table format, just by binding to it, and saves you a lot of work.
 
Share this answer
 
 
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