DtaaGrid sorting problem

G

Guest

Hi
I have a problem when user sorts a column using columnheader clicking.
After sorting when program gets the datasource to access a selected row ,
wrong row is accessed . I think my datasource isn't sorted when user sorts
the gride.Here is a pice of code :

DataTable table=(DataTable)MyDataGrid.DataSource;
string str=table.Rows[MyDataGrid.CurrentRowIndex]["name"].ToString();

How can i solve this problem.
 
G

Guest

Hi Majid,

When you sort a datagrid, it doesnt mean you are actually sorting the
underlying data store object, the datatable. Hence, using the datagrid row
index won't necessarily give you back the correct row, it will merely give
you the row in the underlying store with respect to the original order.

What you need to do is to use the Binding Manager object to find out the
current
object/row that the bound datagrid is now referring to. For example, to get
the current row, use:
((CurrencyManager)BindingContext[datatablename]).Current.
Cast this into a datarow (or your typed one), and then use it.

This article describes very well the windows form data architecture
http://msdn.microsoft.com/library/d...n/html/vbconConsumersOfDataOnWindowsForms.asp

HTH,
Rakesh Rajan
 

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