CurrentRowIndex after sorting

I need to get the value of a column in the current row of a DataGrid. The documentation warns, quite correctly, that the Datagrid could be sorted, so the order of rows is not necessarily the same as inh the underlying dataTable.

Iw there a solution to this problem 

This was recently asked in another thread:
http://www.windowsforms.com/Forums/ShowPost.aspx tabIndex=1&tabId=41&PostID=12601

But the discussion appeared to turn more towards binding. In my case, the code goes something like:

  private ds as DataSet
  private dt as DataTable
  private withevents dg  as System.Windows.Forms.DataGrid
  ....
  dapp = new SqlDataAdapter( "select * from my_table", my_connection )
  dapp.fill( ds, "apps" ) ' name of table in memory will be "apps"
  dt = ds.tables("apps")
  dg = new System.Windows.Forms.DataGrid()
  dg.DataSource = ds.tables("apps")

There is also (unrelated) code which maskes the grid updateable. Then, I have a clicked event for a Drill-Down button, where I use:

  dim rdx as integer = dg.CurrentRowIndex
  dim sel_dr as DataRow = dt.rows( rdx )
  dim sel_app as String = sel_dr("app_code" )
  MsgBox( sel_app,, "Selected row number: " & rdx & "  zero-based" )

My test is:

- the grid displays two rows
- put the cursor on the second row
- sort the grid, in such a manner that the two rows change order
- click on the Drill button
It displays the wrong index and wrong data. It keeps doing so after sorting.
If I don't sort, then it is always correct.

Any thoughts 
Can we somehow access the rows of the DataGrid as opposed to the rows of the DataTable 

Andrew


Answer this question

CurrentRowIndex after sorting

  • Scot Campbell

    In Web Apps , there's no such class as a CurrencyManager.

    A datasource (DataTable, DataSet, .....) does not know which row is currently displayed or active.   
    You'll have to work with the methods of the DataGrid or CurrencyManager.

  • darien_specter

    You'll have to use the CurrencyManager that will be returned when you access the BindingContext of the form for your datatable.


    CurrencyManager cm = (CurrencyManager)this.BindingContext[dt];


    You can then use the position property of the CurrencyManager to get the index of the current row.

  • Benny Au-Young

    thanks for your help. The following now works for me:

            dim c_cell as DataGridcell = dg.currentCell
            dim c_row as integer = c_cell.RowNumber
            dim c_col as integer = c_cell.ColumnNumber
            dim sel_str = dg( c_row, c_col) 
            if IsDBnull( sel_str ) then
                  sel_str = "isDbNull() = true"
            end if
            msgbox( sel_str, , "row: " & c_row & "  ,col: " & c_col & "    zero-based" )

    The above is placed in the code for the DrillDown button, and is not being confused by any sorting which might have occured in between.

  • wrightti

    I just realise there are TWO classes named DataGrid, although the solution to the problem is similar.   I am developing a Windows application, not a Web one.

    I will try using the items collection datagrid( row, col )

  • CurrentRowIndex after sorting