【问题标题】:Removing rows from dataGridView-using dataTable使用 dataTable 从 dataGridView 中删除行
【发布时间】: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


【解决方案1】:

最初,您是数据绑定:

dgv.DataSource = GetTable();

然后你应该继续数据绑定;要么告诉 table 清除自身(并重新填充数据),要么只是将 different 数据表分配给 dgv.DataSource

【讨论】:

  • 我试图将 dgv.DataSource 设置为 null,因为 loadMSXGridView() 是选择删除行之后调用的方法,还是应该做一个完全空白的表?
  • @Spooks 我会做一个空的(不是我经常使用 DataTable)
  • 我试试看,你平时都用什么?
【解决方案2】:

在我有限的数据绑定经验中,编辑数据源并不容易。您应该使用行绑定或其他事件之一来处理数据编辑。但是,如果您想编辑数据源,您基本上必须克隆结构,然后导入行。所以从这个意义上说,你实际上应该回到你的数据源并准确地选择你想要的。

但是..

如果你想过滤/编辑你的数据,你可以这样做:

    DataSet ds1 = [Your Data Source]
DataSet ds2 = new DataSet();

ds2 = ds1.Clone();

for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
{
    if (ds1.Tables[0].Rows[i].ItemArray[0].ToString() != "SomeValue")
    {        
        ds2.Tables[0].ImportRow(ds1.Tables[0].Rows[i]);
    }
}
ds1 = ds2;

【讨论】:

  • 我使用了一个非常相似的 DataTable,但我不得不重置数据源所以 mydgv.DataSource = table2;
【解决方案3】:

如果您只是想添加一个不同的数据表作为数据源,请执行以下操作:

datagridview.DataSource = datatable;
datagridview.Invalidate();

这应该可行,因为我刚刚在我正在处理的项目上做了完全相同的事情:)

【讨论】:

    【解决方案4】:

    我的错,经过我发现的彻底调试后一切正常 备注元素没有被删除,因此通过重新添加相同的项目来删除它。我从 RemarkElement 部分中删除了这些项目,它可以工作,感谢大家的帮助!

    【讨论】:

      【解决方案5】:

      我建议将 DataGridView 的 DataSource 设置为 BindingSource 并改为更改 BindingSource 的 DataSource:

      var table1 = GetDataTable();
      var bindingSource1 = new BindingSource(table1, null);
      dataGridView1.DataSource = bindingSource1;
      
      
      // after reload
      var table2 = GetDataTable();
      bindingSource1.DataSource = table2;
      

      这可以解决大多数问题,因为您不必担心数据是如何绑定的。

      【讨论】:

        猜你喜欢
        • 2021-04-28
        • 2020-10-30
        • 2015-06-13
        • 1970-01-01
        • 2016-11-28
        • 2010-10-30
        • 2013-09-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多