DataGrid Sorting

  • Thread starter Thread starter John Smith
  • Start date Start date
J

John Smith

Hello all:

I am having some major difficulty trying to get my DataGrids to cooperate.

All I wish to do is be able to select a row in a DataGrid and from the
selected row know what item to bind to a second dialog form that pops
up. Everything works fine right now except if the user sorts the
DataGrid which throws the BindingContext Position all out of whack.

Please give me any info you may have on this subject.

Thanks,

- John -
 
Hello John,

You're on the right track with BindingContext, you just need to make one
step further.
The value of Position is correct, it's the way you use it is not. The
Position value corresponds not to a row number in the bound DataTable (to be
more exact, it does unless the grid is sorted).
It rather is an index to a dataview the grid utilizes behind the scenes to
do the sorting.

You can get this dataview from the same CurrencyManager instance you're
querying the position on:

DataView dataView = (DataView)theCurrencyManager.List;
DataRowView selectedRow = dataView[theCurrencyManager.Position];

or, even simpler:

DataRowView selectedRow = (DataRowView)theCurrencyManager.Current;
 
Thanks for the reply, that wasn't exactly what I was looking for but you
helped me find a new path to solve the problem I was having. I thought
that I needed the BindingPosition so that I could then use that same
position for my next form. However, it is much easier to simply find
the row that is selected as you suggested, and then set this row as the
DataBinding for the controls on my next form. I didn't even think of
doing that until you made this suggestion. Before I was passing the
whole DataSet to the next form, binding that to the form, and trying to
utilize the BindingPosition from the previous form. This would work as
long as a user would not sort the columns, but now the other way binds
it as it should. Thanks again.

- John -
 
Back
Top