C# 将 DataTable Row 添加到 DataGridView 无需绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13810056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add DataTable Row to DataGridView without binding
提问by user10001110101
I have a predefined DataGridView that I need to add rows to from a DataTable without data binding. I am trying to use the DataGridView.Rows.Add()method programmatically however, I do not know the column names of the DataTable. The columns in the DataTable are in the same order as the DataGridView but how do I add them to the DataGridView without knowing the column names?
我有一个预定义的 DataGridView,我需要从没有数据绑定的 DataTable 中添加行。我试图以DataGridView.Rows.Add()编程方式使用该方法,但是我不知道 DataTable 的列名。DataTable 中的列与 DataGridView 的顺序相同,但如何在不知道列名的情况下将它们添加到 DataGridView?
采纳答案by JMS
Say your DataGridView exists but has no columns. You can do this:
假设您的 DataGridView 存在但没有列。你可以这样做:
foreach (DataColumn dc in yourDataTable.Columns) {
yourDataGridView.Columns.Add(new DataGridViewTextBoxColumn());
}
Then add the row data:
然后添加行数据:
foreach(DataRow dr in yourDataTable.Rows) {
yourDataGridView.Rows.Add(dr.ItemArray);
}
Now, if the default textbox column isn't sufficient, you might have to create a column with a different cell template.
现在,如果默认文本框列不够用,您可能需要使用不同的单元格模板创建一列。
回答by Fabio
if your DataGridView haven't rows and columns., then just
如果你的 DataGridView 没有行和列,那么就
yourDataGridView.DataSource = yourDataTable
will do all job.
会做所有的工作。
if your DataGridView is already bounded to some datasource(if you using DataTable then I presume DataSource is DataTable),
如果您的 DataGridView 已经绑定到某个数据源(如果您使用 DataTable 那么我认为 DataSource 是 DataTable),
then you need to edit yourDataTable -> add old rows from old DataTable(or from DataGridView if old DataTable not accessible anymore)
那么您需要编辑 yourDataTable -> 从旧 DataTable(如果旧 DataTable 不再可访问,则从 DataGridView 添加旧行)
foreach(DataRow dr in oldDataTable.Rows)
{
yourDataTable.Rows.Add(dr);
}
yourDataGridView.DataSource = yourDataTable;
or edit oldDataTable -> add new rows from yourDataTable, something like:
或编辑 oldDataTable -> 从 yourDataTable 添加新行,例如:
DataTable dtOld = (DataTable)yourDataGridView.DataSource;
foreach(DataRow yourdr in yourDataTable.Rows)
{
dtOld.Rows.Add(yourdr);
}
yourDataGridView.DataSource = dtOld;
回答by spajce
it seems that you want to get the column names from the DataTableand add the rows to DataGridView from DataTable rows
似乎您想DataTable从 DataTable 行中获取列名并将行添加到 DataGridView
DataTable myDataTable = new DataTable();
//adding Columns
myDataTable.Columns.Add("colInt", typeof(int));
myDataTable.Columns.Add("colDate", typeof(DateTime));
myDataTable.Columns.Add("colString", typeof(string));
//adding Rows
myDataTable.Rows.Add(1, DateTime.Now, "Hello World");
//to get columns
foreach (DataColumn col in myDataTable.Columns)
{
var c = new DataGridViewTextBoxColumn() { HeaderText = col.ColumnName }; //Let say that the default column template of DataGridView is DataGridViewTextBoxColumn
dataGridView1.Columns.Add(c);
}
//to get rows
foreach (DataRow row in myDataTable.Rows)
{
dataGridView1.Rows.Add(row[0], row[1], row[2]);
}
anyway there's a shortcut
dataGridView1.DataSource = myDataTable;
反正有捷径
dataGridView1.DataSource = myDataTable;

