转换的DataRow为tablerow [英] Convert DataRow to TableRow
问题描述
就这么简单。不能找到一个简单的解决方案。不知道如果他们的是一个简单的解决方案? :/
Simple enough. Cant find a simple solution. Not sure if their is a simple solution? :/
我有一个DataTable。我基本上是想达到这个...
I have a dataTable. I essentially want to achieve this...
DataTable dt = new DataTable();
dataAdapter.Fill(dt);
TableRow tr = new TableRow();
tr = dt.Rows[0];
但我不能从DataRow的转换为的TableRow!
but i cant convert from DataRow to a TableRow!
帮助!
亚历
推荐答案
如果你的目标是你为什么不使用中继器的用户界面,显示是一个DataTable(或任何其它数据源)中的数据?
if your goal is to show on the UI the data that are within a dataTable(or any other datasource) why don't you use a repeater?
反正你不能只是转换DataTableRow为tablerow但你必须自己做。
看看下面code
anyway you can't just convert the DataTableRow to TableRow but you have to do it yourself. have a look at the below code
private void GenerateTable()
{
DataTable dt = CreateDataTable();
Table table = new Table();
TableRow row = null;
//Add the Headers
row = new TableRow();
for (int j = 0; j < dt.Columns.Count; j++)
{
TableHeaderCell headerCell = new TableHeaderCell();
headerCell.Text = dt.Columns[j].ColumnName;
row.Cells.Add(headerCell);
}
table.Rows.Add(row);
//Add the Column values
for (int i = 0; i < dt.Rows.Count; i++)
{
row = new TableRow();
for (int j = 0; j < dt.Columns.Count; j++)
{
TableCell cell = new TableCell();
cell.Text = dt.Rows[i][j].ToString();
row.Cells.Add(cell);
}
// Add the TableRow to the Table
table.Rows.Add(row);
}
// Add the the Table in the Form
form1.Controls.Add(table);
}
来源:
<一个href=\"http://geekswithblogs.net/dotNETvinz/archive/2009/06/24/fill-asp.net-table-with-data-from-datatable.aspx\" rel=\"nofollow\">http://geekswithblogs.net/dotNETvinz/archive/2009/06/24/fill-asp.net-table-with-data-from-datatable.aspx
这篇关于转换的DataRow为tablerow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!