【发布时间】:2011-03-16 22:10:39
【问题描述】:
我正在尝试删除所有行以重新加载它们(我需要删除所有行)由于某种原因,它没有从我的 datagridview 中删除它们(尽管它也没有添加任何额外的内容)什么都没有发生。我尝试了许多不同的方法,我知道我过去曾这样做过。 (也许是因为它的一天结束)
这是我尝试大量删除的代码
private void loadMSXGridView()
{
BindingSource bs = new BindingSource();
dgv.DataSource = null;
dgv.Refresh();
bs.DataSource = GetTable();
dgv.DataSource = bs;
dgv.Columns[0].Width = 391;
dgv.Columns[1].Width = 30;
}
private DataTable GetTable()
{
DataTable t = new DataTable();
t.Rows.Clear();
t.AcceptChanges();
t.Columns.Add("Accounting Line", typeof(string));
t.Columns.Add("#", typeof(string));
foreach (AMAPnr.RemarkElement re in AMAPnr._RemarkElements)
{
if (re.ElementID == "RM" && re.FreeFlow.StartsWith("*MS"))
{
DataGridViewCell gridCellText;
DataGridViewCell gridCellElement;
gridCellText = new DataGridViewTextBoxCell();
gridCellText.Value = re.FreeFlow;
gridCellElement = new DataGridViewTextBoxCell();
gridCellElement.Value = re.ElementNo.ToString();
t.Rows.Add(gridCellText.Value, gridCellElement.Value);
}
}
return t;
}
我的删除按钮调用 loadMSXGridView,我只需要刷新所有内容,因为我的 datagridview 中的项目与一个元素编号相关联,它不会保持不变
【问题讨论】:
-
为什么不使用另一个 BindingSource 并将其与网格相关联? BindingSource bs = new BindingSource(); bs.DataSource = 你的来源; dgv.DataSource = bs;
-
@HelloWorld 更新有问题,我没有测试,因为编译需要一段时间,它应该工作吗?还是我应该使用不同的方法
标签: c# winforms datagridview