Search code examples
wpfdatagridwpfdatagrid

WPF DataGrid sorts columns by name


I have a Datagrid that gets as Itemssource a DataTable:

dataGrid.ItemsSource = transfer.DataTarget.Table.DefaultView;

But now the main problem is that he resorts the columns by name. As example: I have a Table with this Columns: X Y Z U V

I get displayed: U V X Y Z

How can I disable that. I also tried thisone:

private void LoadTransfer(Transfer transfer)
{
    dataGrid.ItemsSource = transfer.DataTarget.Table.DefaultView;
    //spalten selbst genererieren damit diese nicht nach alphabeth geordnet werden
    int index = 0;
    foreach (DataColumn column in transfer.DataTarget.Table.Columns)
    {
        dataGrid.Columns.Add(new DataGridTextColumn()
        {
            Header = column.ColumnName,
            Binding = new Binding(column.ColumnName),
            DisplayIndex = index++
        });
    }
}

Solution

  • Create a new property that holds the weighting of the values represented as integers. Then on the Column with the sorting issue set it's SortMemberPath to that property.