如何向未绑定的DataGridView添加一行? [英] How to add a row to a unbound DataGridView?
问题描述
我在C#中有一个 DataGridView
,我想以程序化方式添加行。没有数据绑定到网格,但是当我调用 dataGrid.Rows.Add();
它会引发一个 System.InvalidOperationException
。
I have a DataGridView
in C# and I want to add rows in a programmatic way. There is no data bound to the grid but when I call dataGrid.Rows.Add();
it throws a 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.
有人可以帮助我吗?
不确定是否有所作为,但我使用.Net框架3.5。
Not sure if it makes a difference but I use .Net framework 3.5.
推荐答案
我通常会用其他人提供的答案这不是这样,因为答案并不真正有用。
I usually go with answers provided by other people but this is not the case since the answers aren't really helpful.
正如我所说dataGridView1.Rows.Add();抛出异常,AddRange也是如此。
我做了很多检查后发现了答案。显然,如果我添加了很多行/秒(约30),则不喜欢它。
我通过网络接收我的行,所以我创建了一个行池,每隔一秒我更新了datagridview。
这似乎已经修复了没有显示的行和异常。
As I stated "dataGridView1.Rows.Add();" threw an exception and so did 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).
I receive my rows via networking so I created a pool of rows and every second I updated the rows from the datagridview.
This seems to have fixed both the rows not showing up and the exceptions.
感谢您的输入!
这篇关于如何向未绑定的DataGridView添加一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!