简体   繁体   English

如何向datagridview添加新行?

[英]How to add new row to datagridview?

I have DataGridView filled with data from datasource (SQL). 我有DataGridView填充数据源(SQL)的数据。 Now I want to a add new row, but I can't, because new data can't be added to bounded DataGridView... 现在我想添加一个新行,但我不能,因为新数据无法添加到有界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? 怎么做,添加新行而不删除以前的数据?

When you set the DataSource property to null , you are essentially removing all data 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 . 我们假设它是一个DataTable In this case, you'd do something like: 在这种情况下,你会做类似的事情:

DataTable dt = dataGridView1.Source as DataTable;
dt.Rows.Add(new object[] { ... });

And then the DataGridView will pick up on the changes (note that if you are not binding to something that doesn't implement the INotifyCollectionChanged interface , you'll have to call the ResetBindings method to get the grid to refresh). 然后DataGridView将接收更改(请注意,如果您没有绑定到未实现INotifyCollectionChanged接口的内容 ,则必须调用ResetBindings方法以使网格刷新)。

The other option is to let the DataGridView manage the rows. 另一个选择是让DataGridView管理行。 You can do this by manually adding each item using the Add method on the DataGridViewRowCollection returned by the Rows property : 您可以通过使用Rows属性返回的DataGridViewRowCollection上的Add方法手动添加每个项来执行此操作:

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 (或来自底层数据源的数据的其他实现),这对更新底层数据源没有任何作用(这将是一个单独的问题)。

The short answer is, you don't. 简短的回答是,你没有。

When you set your DataSource to null, you've broken the link between your DataGridView and your data source, so its data won't be persisted. 当您将DataSource设置为null时,您已经破坏了DataGridView与数据源之间的链接,因此其数据将不会保留。 You can't add a row to a bound DataGridView because it's supposed to represent the state of the underlying DataSource ; 您不能向绑定的DataGridView添加行,因为它应该表示基础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. 你有效地要求.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

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. 您可以使用数组集合添加任意数量的项目。

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()));
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM