Removing a row at the specified row index after C # sorting

I create 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 the grid

  bs.Sort = "customer DESC"; 

Now I want to delete line 0

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

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

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

+2
source share
2 answers

why not just remove it using a binding source for example

  bs.RemoveAt(0) 

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

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

This is because BindingSource.Insert simply calls System.Collections.IList.Insert() on the base list. The main list is a DataView. Insert on Dataview implementation -

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

You can show it with

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

delete in the binding source, not in the dataset

+2
source

Source: https://habr.com/ru/post/1397125/


All Articles