Getting wrong index after sorting columns DataGrid

Posted by: nickjohngray on 7 August 2017, 7:12 am EST

  • Posted 7 August 2017, 7:12 am EST


    My DataGrid is bound to a DataTable and after a column sort in the DataGrid is performed it returns the wrong ROW index, so the mapping between the model and the view are inconsistent, ( am i missing something ? )

    I Access the row like this:

    1. Setup grid and bind it to a dynamic data source

    DataGrid myGrid = new DataGrid();

    DataTable myGridData = new DataTable();

    // add columns to table

    // add new row(s) data to table

    // bind grid to model

    myGrid.ItemsSource = myGridData;

    2. Get the current row the user clicked on

    iRow = myGrid.CurrentRow.Index; ( or should i use display Index)

    3. then i go to the model to get the data from it

    string id = myGridData.Rows[. iRow ] [ "COL_ID"];

    // this returns the wrong index !

    Please help Nick

  • Posted 7 August 2017, 7:12 am EST

    It would help if you can attach a sample project.

    BTW, you can get the data item reference by using the Row.DataItem property, maybe that's enough.


  • Posted 7 August 2017, 7:12 am EST


    Yes i can get the Row.DataItem by myGrid.CurrentRow.DataItem, ( that is an instance of the dynamic class created by DataTable ).

    But how do i find out what row on the DataGrid that maps to? as I can't just use MyGridData[ myGrid.CurrentRow.Index ] [ "COL_0"] .

    Basically is there another way to do this myGrid.CurrentRow.Index ] [ "COL_0"] that works ( returns the correct row data) ?

    It seems that some other grid you guys make has had this issue (

    , maybe i need to do a similer thing, if so how?

    If you still need same code let me know ( as i need to create a sandbox for you)


  • Posted 7 August 2017, 7:12 am EST

    Going one step backwards... accessing the data item by the row index should work fine.

    The issue is probably that you are not accessing the right collection (i.e. accesing the DataTable instead of the DataView); or maybe the rows collection & the underlying collection has pending synchronization (i.e. you have sorted but the the rows were not reloaded yet).

    That's why I wanted to see the code...


  • Posted 7 August 2017, 7:12 am EST


    I think that replacing this method makes the trick:

    private void myGrid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)


    var rowView = myGrid.CurrentRow.DataItem as DataRowView;

    C1.Silverlight.C1MessageBox.Show("myGridData.Rows[myGrid.CurrentRow.DisplayIndex]["id_ROW_NUM"] ="

    + rowView.GetData("id_ROW_NUM")

    + "nmyGrid.CurrentRow.DisplayIndex= " + myGrid.CurrentRow.Index);



  • Posted 7 August 2017, 7:12 am EST


    I have attached a simple for you,

    I have tried going backwrds but could not work it out,

    Please help


  • Posted 7 August 2017, 7:12 am EST


    Thanks, you really saved my ass, thanks man! It works!


  • Posted 7 August 2017, 7:12 am EST


    Just one other Question on this, how do i insert a row in the model so it inserts it into the same place on the DataGrid ?

    I tried using myGridData.Rows.Insert(iRowNum, r) but it appends the row to the end of the DataGrid

    Thanks Nick

  • Posted 9 December 2018, 9:35 pm EST

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels