排序后删除行给定行索引c# [英] deleting row a given row index after sorting c#

查看:222
本文介绍了排序后删除行给定行索引c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用bindnig源构建我的datagrid:

  SqlDataAdapter adapter = new SqlDataAdapter(Datenbank.cmd); 
dataSet1.Tables.Clear();
adapter.Fill(dataSet1,Table);
bs = new BindingSource();
bs.DataSource = dataSet1.Tables [Table];
dataGridView1.DataSource = bs;

现在我排序网格

  bs.Sort =customer DESC; 

现在我要删除第0行

  dataSet1.Tables [0] .Rows.RemoveAt(0); 

但是,排序前位置0的行将被删除,而不是现在的行在位置0



//编辑:是相似的 test.Tables [0] .Rows.InsertAt newRow,0);

解决方案

为什么不使用绑定源删除它
例如

  bs.RemoveAt(0)

关于 test.Tables [0] .Rows.InsertAt(newRow,0);



c $ c> BindingSource.Insert(int,object)或 BindingSource.List.Insert(int,object)好的,但是当源是DataSet时不支持。



这是因为BindingSource.Insert只是调用code> System.Collections.IList。在底层列表中插入()。底层列表是DataView。在Dataview上插入的实现是

  private void System.Collections.IList.Insert(int index,object value)
{
throw ExceptionBuilder.InsertExternalObject();
}

您可以通过

  System.Data.DataView dv = bs.List作为DataView; 
System.Collections.IList list = dv;
list.Insert(0,newRow); // BANG InsertExternalObject exception


I build up my datagrid with bindnig source:

    SqlDataAdapter adapter = new SqlDataAdapter(Datenbank.cmd);
    dataSet1.Tables.Clear();
    adapter.Fill(dataSet1, "Table");
    bs = new BindingSource();
    bs.DataSource = dataSet1.Tables["Table"];
    dataGridView1.DataSource = bs;

Now I sort grid

    bs.Sort = "customer DESC";

Now I want to remove row 0

    dataSet1.Tables[0].Rows.RemoveAt(0);

However, the row which was at position 0 before sorting will be deleted, not the row which now is on position 0

//EDIT : is there similar for test.Tables[0].Rows.InsertAt(newRow, 0); ?

解决方案

why not just remove it using the binding source e.g.

  bs.RemoveAt(0)

Regarding test.Tables[0].Rows.InsertAt(newRow, 0);

BindingSource.Insert(int, object) or BindingSource.List.Insert(int, object) looks good but it isn't supported when the source is a DataSet.

This is because BindingSource.Insert just calls the System.Collections.IList.Insert() on the underlying list. The underlying list is a DataView. The implementation of Insert on Dataview is

private void System.Collections.IList.Insert(int index, object value)
{
    throw ExceptionBuilder.InsertExternalObject();
}

You can show this by

 System.Data.DataView dv = bs.List as DataView;
 System.Collections.IList list = dv;
 list.Insert(0,newRow); //BANG InsertExternalObject exception

这篇关于排序后删除行给定行索引c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆