C# 如何向datagridview添加新行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9608647/
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
How to add new row to datagridview?
提问by Bodi
I have DataGridView filled with data from datasource (SQL). Now I want to a add new row, but I can't, because new data can't be added to bounded DataGridView...
我有 DataGridView 填充了来自数据源 (SQL) 的数据。现在我想添加新行,但我不能,因为新数据无法添加到有界 DataGridView...
I was trying to :
我试图:
dataGridView1.Source = null;
dataGridView1.Rows.Add("1");
but it clears my previous data in table. How to do it, to add new row without deleting previous data?
但它清除了我以前在表中的数据。怎么做,添加新行而不删除以前的数据?
采纳答案by casperOne
When you set the DataSourcepropertyto null, you are essentially removing alldata from the DataGridView(since it doesn't know what to bind to anymore).
当您将DataSource属性设置为 时null,您实际上是从 中删除所有数据DataGridView(因为它不再知道要绑定到什么)。
You have two options here. The first is to update the underlying data source. Let's assume that it's a DataTable. In this case, you'd do something like:
您在这里有两个选择。首先是更新底层数据源。让我们假设它是一个DataTable. 在这种情况下,您可以执行以下操作:
DataTable dt = dataGridView1.Source as DataTable;
dt.Rows.Add(new object[] { ... });
And then the DataGridViewwill pick up on the changes (note that if you are not binding to something that doesn't implement the INotifyCollectionChangedinterface, you'll have to call the ResetBindingsmethodto get the grid to refresh).
然后DataGridView将接受更改(请注意,如果您没有绑定到未实现INotifyCollectionChangedinterface 的内容,则必须调用该ResetBindings方法以使网格刷新)。
The other option is to let the DataGridViewmanage the rows. You can do this by manually adding each item using the Addmethodon the DataGridViewRowCollectionreturned by the Rowsproperty:
另一种选择是让DataGridView管理行。您可以通过手动添加使用的每个项目做到这一点Add的方法对DataGridViewRowCollection通过返回的Rows属性:
foreach (var item in source)
{
dataGridView1.Rows.Add("1", "2", "3", ...);
}
I wouldn't say the second solution is optimal, but it will work.
我不会说第二个解决方案是最优的,但它会起作用。
Finally, assuming you are binding to a DataTable(or some other materialization of the data from an underlying data source), this doesn't do anything about to updating underlying data source (that would be a separate question).
最后,假设您绑定到一个DataTable(或来自底层数据源的数据的其他一些具体化),这对更新底层数据源没有任何作用(那将是一个单独的问题)。
回答by Jim Dagg
The short answer is, you don't.
简短的回答是,你没有。
When you set your DataSourceto null, you've broken the link between your DataGridViewand your data source, so its data won't be persisted. You can't add a row to a bound DataGridViewbecause it's supposed to represent the state of the underlying DataSource; you're effectively asking .net to make your table out of sync with its backing store, defeating the purpose of databinding in the first place.
当您将 your 设置DataSource为 null 时,您已经断开了您DataGridView和数据源之间的链接,因此其数据不会被持久化。您不能向边界添加一行,DataGridView因为它应该代表底层的状态DataSource;您实际上是在要求 .net 使您的表与其后备存储不同步,这首先违背了数据绑定的目的。
If you want to add a row to the backing store, you should be adding a row in the DataSource, not in your DataGridView.
如果您想向后备存储添加一行,您应该在 中添加一行,而DataSource不是在您的DataGridView.
回答by Andrew
maybe you want to do it manually and detailed? Something like this?
也许你想手动和详细地做?像这样的东西?
DataSet ds = new DataSet();
OleDbDataAdapter adapter = null;
adapter = new OleDbDataAdapter("SELECT * FROM WHERE", conn);
adapter.Fill(ds);
dataGridView1.ColumnCount = 5; //how many columns returns your SQL query? starts with 0
dataGridView1.Columns[0].Name = "COl-1";
dataGridView1.Columns[1].Name = "COl-2";
dataGridView1.Columns[2].Name = "COl-3";
dataGridView1.Columns[3].Name = "COl-4";
dataGridView1.Columns[4].Name = "COl-5";
DataTable dt = ds.Tables[0];
foreach (DataRow dr in dt.Rows)
{
dataGridView1.Rows.Add(
(dr["COL_HEADER_NAME1"].ToString()),
(dr["COL_HEADER_NAME2"].ToString()),
(dr["COL_HEADER_NAME3"].ToString()),
(dr["COL_HEADER_NAME4"].ToString()),
(dr["COL_HEADER_NAME5"].ToString()));
}
回答by Hemal Rathod
You just add rows by using add method of rows collection
您只需使用行集合的添加方法添加行
me.datagridview1.rows.add("first","second","third");
You can add any amount of items with array collection.
您可以使用数组集合添加任意数量的项目。

