Programmatically select row in bound datagrid

  • Thread starter Thread starter Jay Ayliff
  • Start date Start date
J

Jay Ayliff

Hello,

If I have a DataGrid bound to a DataTable. The grid can be sorted, so
there's no obvious link between row number in the table and row number in
the DataTable. If the user clicks a row in the table I know how to select
the corresponding row in the DataTable, but how do the reverse: I
programmatically select a row in the DataTable, then want to highlight the
row in the DataGrid.

Regards

Jay Ayliff
Stalis Ltd
 
Hey Jay, if all you need to worry about is selecting the appropriate item in
the data grid then you can do it like this...

in the mouseup handler for the datagrid:

\\\
me.myDataGrid.Select(me.myDataGrid.CurrentRowIndex)
///

the underlying datasource of the datagrid will have a currency manager
associated with it which will take care of synchronizing the CurrentRowIndex
based on the selected item in the underlying dataview.

you can access the currencymanger of the datagrid's datasource to manipulate
it's position property etc via code.

\\\
dim myCM as CurrencyManager =
Directcast(me.myDataGrid.BindingContext(myGridsDataSourceTable),
CurrencyManager)

'--- set the current position of the currency manager ---'
myCM.position = 5

me.myDataGrid.Select(me.myDataGrid.CurrentRowIndex)
///

hope this helps,

jim
 
Hi Jim,

I want to do exactly the opposite. My program finds a row in the DataTable and I want to highlight the corresponding row in the DataGrid. The user doesn't click on the grid at all. I want the program to highlight the row and make sure it is displayed.

Jay
 
Hi Jay,

it doesn't matter how you select the current row of the datasource, so long
as it's set. try something like this:

\\\
'--- some code to set the datasource of the datagrid ---'

'--- get access to the datasources currencymanager ---'
Dim myCm As CurrencyManager =
CType(Me.BindingContext(DirectCast(Me.myDataGrid.DataSource, DataView)),
CurrencyManager)

'--- set the current row in the datasource ---'
myCm.Position = 2 'or however your program selects the row

'--- select the datagrid row corresponding to the currently selected
datasource row ---'
me.myDataGrid.Select(me.myDataGrid.CurrentRowIndex)
///

even if your'e binding a datatable to the datagrid, you can pull the
datasource out as a dataview, as the grid is actually binding to the
DefaultView property of the table that you select.

it doesn't matter how you select the current row in the underlying
datasource, the currencymanager will take care of setting the
CurrentRowIndex in the datagrid for you. I just implemented it from the
mouseUp event as that's the way i've been working with it.

Since you're taking the dataview of the datagrid to determine the currency
manager it will take into accout the current sort applied to the grid.

hope this helps,

jim


Jay Ayliff said:
Hi Jim,

I want to do exactly the opposite. My program finds a row in the DataTable
and I want to highlight the corresponding row in the DataGrid. The user
doesn't click on the grid at all. I want the program to highlight the row
and make sure it is displayed.
 
Back
Top