gpt4 book ai didi

c# - 如何在不引发 "Row provided already belongs to a DataGridView"错误的情况下缓存行?

转载 作者:行者123 更新时间:2023-11-30 22:50:25 24 4
gpt4 key购买 nike

我想在“刷新”之间缓存 DataGridView 行,即在 Rows.Clear()Columns.Clear() 之间.然而,似乎调用 Clear() 方法并没有解除数据与 DataGridView 实例的绑定(bind),举个例子,


public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

DataGridViewRow cachedRow = new DataGridViewRow();

private void button1_Click(object sender, EventArgs e)
{
this.dataGridView1.Rows.Clear();
this.dataGridView1.Columns.Clear();
DataGridViewColumn aColumn = new DataGridViewTextBoxColumn();
this.dataGridView1.Columns.Add(aColumn);
this.dataGridView1.Rows.Add(cachedRow);
}
}

这是在包含 DataGridViewButton 的表单上完成的。单击该按钮两次会出现“提供的行已属于 DataGridView”错误。

网上有一些关于此的讨论表明它可能是一个错误,但那是在 2004 年左右。

最佳答案

一旦某行成为 gridview 的一部分,您就无法重新添加它。该行本身 keeps track of what DataGridView它在里面。我建议制作缓存行的副本并将副本添加到 View 中。由于您每次都创建一个新副本,因此它不会出现在 View 中。或者,您可以遍历并仅删除那些尚未从 View 中缓存的行,而将缓存的行留在后面,这样您就不需要重新添加它。

关于c# - 如何在不引发 "Row provided already belongs to a DataGridView"错误的情况下缓存行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/598627/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com