2011-03-16 57 views
2

我想删除所有行来重新加载它们(我需要删除所有行)由于某种原因,它不会从我的datagridview中删除它们(它不添加任何额外的任何)发生。我尝试了很多不同的方法,我知道我过去做过这些。 (也许是因为它的一天结束)从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的项目相关联以要素号,这将不会保持不变

+0

为什么不使用另一个BindingSource并将它关联到网格? BindingSource bs = new BindingSource(); bs.DataSource = YourSource; dgv.DataSource = bs; – WorldIsRound 2011-03-16 22:15:14

+0

@HelloWorld更新问题,我没有测试,因为它需要一段时间才能编译,只要它工作?或者我应该使用不同的方法 – Spooks 2011-03-16 22:19:42

+0

我会试一试! – WorldIsRound 2011-03-16 22:22:38

回答

0

是我的错,一切都运行throughough调试后我发现工作 备注元素没有被删除,因此它通过添加相同的项目被删除。我从RemarkElement部分删除项目,它的工作原理,感谢您的帮助每个人!

1

最初,你是数据绑定:

dgv.DataSource = GetTable(); 

你应该再继续数据绑定;或者告诉自行清除(并重新填充数据),或者将不同的数据表分配给dgv.DataSource

+0

我试图将dgv.DataSource设置为null,因为loadMSXGridView()是选择删除行后调用的方法,还是应该执行完全空白的表? – Spooks 2011-03-16 22:21:23

+0

@Spooks我会做一个空的(不是我使用DataTable太多) – 2011-03-16 22:27:45

+0

我会试一试,你通常使用什么? – Spooks 2011-03-16 22:28:55

1

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

但是..

如果要过滤/此处修改您的数据是如何可以做到这一点:

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; 
+0

我用了一个非常相似的DataTable,但我不得不重置数据源,所以mydgv.DataSource = table2; – Rob 2015-01-09 19:12:02

1

如果你仅仅只是想添加不同的数据表作为数据源,然后执行:

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

这应该工作,正如我刚才做了一个项目我在工作的完全一样的东西一分钟:)

0

我建议设置你的DataGridView到的BindingSource的DataSource和改变的BindingSource的数据源,而不是:这解决了大多数

var table1 = GetDataTable(); 
var bindingSource1 = new BindingSource(table1, null); 
dataGridView1.DataSource = bindingSource1; 


// after reload 
var table2 = GetDataTable(); 
bindingSource1.DataSource = table2; 

问题,因为您不必担心数据的绑定方式。