Click here to Skip to main content
16,018,158 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I bind a selected record value from DataGridView to WinForm Controls using C#.net
by this code:
C#
Textbox.text = DataGridView.Rows[e.RowIndex].Cells[0].Value.ToString() // Here Cells[0] for 1st column

When I sort columns the order of rows changes and this code uses RowIndex of
DataGrid. How solve this problem?
Posted
Updated 1-Apr-12 2:52am
v4
Comments
OriginalGriff 29-Mar-12 3:26am    
Why is this a problem?
It is the designed, expected behaviour.
What did you expect it to do?
What are you trying to achieve that makes you think this is wrong?
fct2004 31-Mar-12 22:29pm    
Why is this a problem, shouldn't the event handler be giving you the correct row index when it responds to the event the set it off?

1 solution

Hello,

Have a collection, for example List<test> MyList</test>
And bind it to DataGrid

If you sort the columns, the List will stay unsorted.
C#
Textbox.text = MyList[0].ToString();

If you want to modify the DataGrid, (for example Deleting a record):

(
you must modify MyList instead of it. Then ReBind it to the DataGrid and apply the possible sorting to DataGrid
)

OR

(
Implement the INotifyPropertyChanged. Look at this: INotifyPropertyChanged Interface
)
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900