views:

93

answers:

2

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();
A: 

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
how to move that index?
EEE
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
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
+1  A: 

I found the answer. Here is the link

EEE