1

I have a datagridview whose source is a datatable. There are orders and food names columns. I insert new values into the this datagridview and delete from the table. when I select one row and delete it, selected color moves to the top, and if I want to delete one more order for same food, I have to reclick it each time. How can I solve this problem? by the way I update the datagridview after inserting or deleting from sql, like this;

ds.Clear();
da.Fill(ds);
dataGridView2.DataSource = ds.Tables[0];

da and ds is defined like this;

 da = new SqlDataAdapter(sqlcommand);
 ds = new DataSet();
EEE
  • 4,536
  • 3
  • 28
  • 34

1 Answers1

0

Perhaps if you store the index of your current selection into a variable before deletion, then move to this index after your DataSet has been filled.

Will Marcouiller
  • 23,773
  • 22
  • 96
  • 162
  • If I remember correctly, both the DataGridView.DataSource and DataSet.Tables[0] will share the same refenrece altogether. Then if so, moving the DataSet.Tables[0].Rows[savedIndex] should do the job. Otherwise, there is also an object in Windows Form that is owned by a Form instance that is BindingContext[BindingSource/DataSource] something around it, that will allow you to use a CurrencyManager, then throguh this CurrencyManager you will be able to get the position index of the current selected item/row within the DataSource. – Will Marcouiller Sep 26 '09 at 17:44
  • I thought you wanted to do it from within the data source. But indeed at any time you can get the Rows collection of a DataGridView, etc. as mentioned in the link you posted. What's important is that you achieved what you wanted to do. =) – Will Marcouiller Sep 26 '09 at 20:33