How do you map DataGrid rows to it's DataSource when sorting a DataGrid?

A

aualias

I have a DataGrid and I want the user to be able to delete rows. When the
grid is not sorted the index of the DataGrid corresponds to the row in the
DataTable that is the datasource.

However, if I sort, the DataGrid seems to be keeping track of the order.
The CurrencyManager acts as if the DataGrid is not sorted and the wrong row
gets deleted.

How can I keep track of the sorting so that I can delete the correct row?

Thanks.

David
 
B

Bart Mermuys

Hi,

aualias said:
I have a DataGrid and I want the user to be able to delete rows. When the
grid is not sorted the index of the DataGrid corresponds to the row in the
DataTable that is the datasource.

However, if I sort, the DataGrid seems to be keeping track of the order.
The CurrencyManager acts as if the DataGrid is not sorted and the wrong
row gets deleted.

That's because it's actually (internally) bound to a DataView and not a
DataTable.
How can I keep track of the sorting so that I can delete the correct row?

- The CurrencyManager has a property called Current, which returns the
current DataRowView.

CurrencyManager cm =
(CurrencyManager)BindingContext[dataGrid1.DataSource,dataGrid1.DataMember];
DataRowView currentDRV = (DataRowView)cm.Current;

- DataRowView has a Delete method, but if you want you can get the DataRow
from it:

DataRow currentRow = currentDRV.Row;

HTH,
Greetings
 
A

aualias

Bart,

Thanks. That makes sense. I shall give it a try.

David


Bart Mermuys said:
Hi,

aualias said:
I have a DataGrid and I want the user to be able to delete rows. When the
grid is not sorted the index of the DataGrid corresponds to the row in the
DataTable that is the datasource.

However, if I sort, the DataGrid seems to be keeping track of the order.
The CurrencyManager acts as if the DataGrid is not sorted and the wrong
row gets deleted.

That's because it's actually (internally) bound to a DataView and not a
DataTable.
How can I keep track of the sorting so that I can delete the correct row?

- The CurrencyManager has a property called Current, which returns the
current DataRowView.

CurrencyManager cm =
(CurrencyManager)BindingContext[dataGrid1.DataSource,dataGrid1.DataMember];
DataRowView currentDRV = (DataRowView)cm.Current;

- DataRowView has a Delete method, but if you want you can get the DataRow
from it:

DataRow currentRow = currentDRV.Row;

HTH,
Greetings

Thanks.

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top