Intriguing DataGrid column header behaviour? RRS feed

  • General discussion

  • I have a DataGrid with two columns.  The first's header is Name and the second is Description.  When the datagrid is first loaded, dg.Columns[0].Header is Name and dg.Columns[1].Header is Description, which is correct.  However if I swap the column positions in such a way that Description is now first, then dg.Columns[0].Header is still Name.  Should it not be Description now?


    Wednesday, August 31, 2011 1:47 PM

All replies

  • Use the DisplayIndex to find the original index. When you reorder only the displayindex is rearranged.

    Thursday, September 1, 2011 1:35 PM
  • I swap the column positions in such a way that Description is now first, then dg.Columns[0].Header is still Name. 

    Hi Stratoblue,

    How do you swap the columns' position? What is the context of the DataGrid? And where is the datas of the DataGrid come from, use binding or something else? To troubleshoot the issue, please post some code snippets here to help us to reproduce the problem.

    Best Regards,

    Friday, September 2, 2011 6:21 AM
  • Hi Haixia,

    Thanks for your reply.  The user swaps th columns at runtime.  I need to monitor the new location of a particular column when the user does that.  I thought it would be as easy as simply tracking the column header.  It turns out that the columnindex stays the same as the original regardless of where the user positions it.  Alt_fo observed correctly that DisplayIndex changes, but this is a property of the DataGridColumn and not the DataGrid itself so it is harder to code.

    To answer your other questions: I set the dg.ItemsSource to a PagedCollectionView.  The data come from a RIA call.  The cell data itself is databound.  The columns are not autogenerated.  I will try to provide a simplified XAML next post.

    Friday, September 2, 2011 10:48 AM
  • The user swaps th columns at runtime.

    Hi Stratoblue,

    Can you post the code snippets on how you swap the columns at runtime to better explain the issue? In addtion, you may have a look at this thread on how to swap datagrid ItemsSource data:


    Best Regards,

    Monday, September 5, 2011 3:38 AM
  • Hi Stratoblue,

       Actuallt its easy to get the columns and identify the displayindex. I am not sure what you trying to do with the datagrid. May be if you can share the code can help to fix it.

       Once you identify the datacolumn i dont know what you want to do

    Monday, September 5, 2011 11:42 AM
  • I  will post some code shortly.

    Tuesday, September 6, 2011 10:57 PM