简体   繁体   English

如何向未绑定的DataGridView添加行?

[英]How to add a row to a unbound DataGridView?

I have a DataGridView in C# and I want to add rows in a programmatic way. 我在C#中有一个DataGridView ,我想以编程方式添加行。 There is no data bound to the grid but when I call dataGrid.Rows.Add(); 没有绑定到网格的数据但是当我调用dataGrid.Rows.Add(); it throws a System.InvalidOperationException . 它抛出一个System.InvalidOperationException

I looked all over the internet and I only found this problem for people who have data bound to it. 我浏览了整个互联网,我发现这个问题对于有数据绑定的人来说。 I want the grid to be controlled completely from the code. 我希望从代码中完全控制网格。

Could anyone help me with this please? 有人可以帮我这个吗?

Not sure if it makes a difference but I use .Net framework 3.5. 不确定它是否有所作为,但我使用.Net framework 3.5。

Assuming you have created the columns, either with the designer or by code you can do: 假设您已经使用设计器或代码创建了列,您可以执行以下操作:

var row = (DataGridViewRow)myDataGridView.RowTemplate.Clone();
row.CreateCells(myDataGridView, "I'm Cell 1", "I'm Cell 2", "etc.");
myDataGridView.Rows.Add(row);

Ideally if you are adding many rows you would create an array of rows upfront and call AddRange(rows); 理想情况下,如果要添加许多行,则需要预先创建一个行数组并调用AddRange(rows); instead. 代替。

Example: 例:

void PopulateGrid()
{
    //Consider Suspend-Resume Layout, YMMV.
    var rows = myData.Select(data => CreateRow(data)).ToArray();
    myDataGridView.Rows.AddRange(rows);
}

DataGridViewRow CreateRow(MyData data)
{
    var row = (DataGridViewRow)myDataGridView.RowTemplate.Clone();
    row.CreateCells(myDataGridView, data.Text, data.Date, date.Value);
    return row;
}

the easiest example i could give is: 我能给出的最简单的例子是:

/// <summary>
/// Shows example usage of Add method on Rows.
/// </summary>
void M()
{
    //
    // n is the new index. The cells must also be accessed by an index.
    // In this example, there are four cells in each row.
    //
    int n = dataGridView1.Rows.Add();

    dataGridView1.Rows[n].Cells[0].Value = title;
    dataGridView1.Rows[n].Cells[1].Value = dateTimeNow;

    //
    // The second cell is a date cell, use typeof(DateTime).
    //
    dataGridView1.Rows[n].Cells[1].ValueType = typeof(DateTime);
    dataGridView1.Rows[n].Cells[2].Value = wordCount;
}

I usually go with answers provided by other people but this is not the case since the answers aren't really helpful. 我通常会选择其他人提供的答案,但事实并非如此,因为答案并不真正有用。

As I stated "dataGridView1.Rows.Add();" 正如我所说的“dataGridView1.Rows.Add();” threw an exception and so did AddRange. 抛出异常,AddRange也是如此。
I found out the answer after doing a lot of checks. 经过大量检查后我找到了答案。 Apparently .Net does not like it if I add a lot of rows/second (about 30). 显然.Net不喜欢它,如果我添加很多行/秒(大约30)。
I receive my rows via networking so I created a pool of rows and every second I updated the rows from the datagridview. 我通过网络接收我的行,所以我创建了一个行池,每秒我都会更新datagridview中的行。
This seems to have fixed both the rows not showing up and the exceptions. 这似乎修复了未显示的行和异常。

Thank you for the input anyway! 无论如何,谢谢你的输入!

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

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