在DataGridView(C#)的中间插入行 [英] Insert row in middle of DataGridView (C#)

查看:615
本文介绍了在DataGridView(C#)的中间插入行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在特定索引的DataGridView中插入一个新的DataGridViewRow。如果我只是创建一个新的行,如

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

我不会得到DataGridView的设置,例如我的单元格将如果我使用,这不会发生。

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];

这将在index中插入1个空模板行,然后只需访问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(C#)的中间插入行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆