簡體   English   中英

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

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

我想在我的DataGridView的特定索引處插入一個新的DataGridViewRow。 如果我只是創建一個新的行

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

我不會獲得DataGridView的“設置”,例如我的“單元格”將為0。如果使用,則不會發生這種情況。

DataGridView1.Add();

但是話又說回來,然后我無法選擇列表中我想要的位置...

無論如何,有沒有要結合這些優勢?

/缺口

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

這將在“索引”處插入1個空模板行,然后僅訪問“索引”中的行。 第二行用於訪問剛添加的行。

如果您知道所需的行值,也可以使用以下方法來縮短:

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

只需確保它與網格中的列順序同步,即可自動使行與這些字段一起使用。

DataGridViewDataGridRowView只是數據源和數據源中一行的直觀表示。 在將新行添加到數據源后,應在DataView中顯示一個新的可視行。

如果要在添加新行時獲取視圖行,請處理Row Transactions事件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM