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

    Hi,


    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


     


     


     


     


     


     

  • Replied 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.

    Regards

  • Replied 7 August 2017, 7:12 am EST

    Hi,


    I have attached a simple for you,


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


    Please help


    Nick


     


    2010/04/231791_c1datagrid_bug.rar
  • Replied 7 August 2017, 7:12 am EST

    Hi,


     


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


     


    Nick


     

  • Replied 7 August 2017, 7:12 am EST

    Hi,


     


    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


     

  • Replied 7 August 2017, 7:12 am EST

    Hi,


     


    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 ( http://helpcentral.componentone.com/CS/forums/p/38149/218570.aspx#218570)


    ,  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)


    Nick


     


     


     


     

  • Replied 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...

    Regards

  • Replied 7 August 2017, 7:12 am EST

    Hi,

    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);
    }

    Regards

  • Replied 9 December 2018, 9:35 pm EST

    Check here more on...C# DatagridView

    http://csharp.net-informations.com/datagridview/csharp-datagridview-tutorial.htm
Need extra support?

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

Learn More

Forum Channels