C# DataGridView控件处于虚拟模式时无法添加行?
当我尝试在虚拟模式下向DataGridView添加新行时,如下所示:C# DataGridView控件处于虚拟模式时无法添加行?,c#,winforms,datagridview,C#,Winforms,Datagridview,当我尝试在虚拟模式下向DataGridView添加新行时,如下所示: dataGridViewX1.Rows.Add(new string[] { "Parker", "Seattle" }); // add information to the underlying source this.dataGridView.RowCount++; 出现以下异常: 当 DataGridView控件处于虚拟状态 模式 当DataGridView处于(DataGridView.VirtualMode=t
dataGridViewX1.Rows.Add(new string[] { "Parker", "Seattle" });
// add information to the underlying source
this.dataGridView.RowCount++;
出现以下异常:
当
DataGridView控件处于虚拟状态
模式
当
DataGridView
处于(DataGridView.VirtualMode=true
)中时,这意味着DataGridView
的内容是通过DataGridView
上公开的事件从其他源提供的。如果希望DataGridView处于虚拟模式,则需要将新信息添加到基础源,然后告诉DataGridView
还有一行,如下所示:
dataGridViewX1.Rows.Add(new string[] { "Parker", "Seattle" });
// add information to the underlying source
this.dataGridView.RowCount++;
如果您不打算在虚拟模式下使用
DataGridView
,可以通过将DataGridView.VirtualMode
设置为false
,来关闭虚拟模式。您需要使用数据源添加新行,它将自动显示在DataGridView中。您可能已经完成了其他问题建议的所有操作,但是将您的DataGridView.DataSource设置为某个地方
不能在虚拟机中设置它,否则在尝试设置行数时会收到类似的消息,而在尝试添加行数时会收到该消息
如果您以前使用的“数据源”作为一个“邮箱”实例变量,您的事件处理程序可以使用它来处理数据源,请考虑将其填充到TAG属性中,并使用该属性所需的引用(使用适当的类型转换)。