简体   繁体   中英

Insert row in middle of DataGridView (C#)

I would like to insert a new DataGridViewRow into my DataGridView at a specific index. 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.

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'. 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. A new visual row should be displayed in your DataView after a new row is added to your data source.

If you want to get the view row when new row is added handle RowsAdded event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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