简体   繁体   English

在DataGridView(C#)的中间插入行

[英]Insert row in middle of DataGridView (C#)

I would like to insert a new DataGridViewRow into my DataGridView at a specific index. 我想在我的DataGridView的特定索引处插入一个新的DataGridViewRow。 If I just create a new Row like 如果我只是创建一个新的行

DataGridViewRow dgwr = new DataGridViewRow();
datagridview1.Rows.Insert(index, dgwr);

I will not get the "settings" of the DataGridView, like for example my "Cells" will be 0. This does not happen if I use. 我不会获得DataGridView的“设置”,例如我的“单元格”将为0。如果使用,则不会发生这种情况。

DataGridView1.Add();

But then again, then I cant chose where in the list I would like my post... 但是话又说回来,然后我无法选择列表中我想要的位置...

Is there anyway to combine these advantages? 无论如何,有没有要结合这些优势?

/Nick /缺口

grid.Rows.Insert(index, 1);
var addedRow = grid.Rows[index];

This inserts 1 empty templated row at 'index', and then simply accesses row in 'index'. 这将在“索引”处插入1个空模板行,然后仅访问“索引”中的行。 The 2nd line is for accessing the just-now-added row. 第二行用于访问刚添加的行。

Can also shorten if you know your wished row values with: 如果您知道所需的行值,也可以使用以下方法来缩短:

grid.Rows.Insert(index, FirstName, LastName, BirthDate, Etc);

Just have to make sure it is synced with the columns order in the grid, and it makes the row automatically with these fields. 只需确保它与网格中的列顺序同步,即可自动使行与这些字段一起使用。

DataGridView and DataGridRowView are just visual representations of your data source and one row in your data source. DataGridViewDataGridRowView只是数据源和数据源中一行的直观表示。 A new visual row should be displayed in your DataView after a new row is added to your data source. 在将新行添加到数据源后,应在DataView中显示一个新的可视行。

If you want to get the view row when new row is added handle RowsAdded event. 如果要在添加新行时获取视图行,请处理Row Transactions事件。

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

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