I have a DataGrid bound to a DataTable. For various reasons, I don't want to allow users to sort the data, but I want to sort the data once by "Username" as well as resize the columns.

I use DataTable.DefaultView.Sort = "Username" before binding it to DataGrid.

If I then create a DataGridTableStyle to set column widths for my DataGrid and add it to DataGrid, it overwrites the DataGrid's AllowSorting setting.

If I then set DataGridTableStyle.AllowSorting = False, it reverts back my sorting on DataTable for some reason. This behavior is undocumented and not mentioned anywhere on the web. The AllowSorting from DataGrid doesn't behave this way, so why the silliness? How do I get around it?

I have a DataGrid bound to a DataTable. For various reasons, I don't want to allow users to sort the data, but I want to sort the data once by "Username" as well as resize the columns.

I use DataTable.DefaultView.Sort = "Username" before binding it to DataGrid.

If I then create a DataGridTableStyle to set column widths for my DataGrid and add it to DataGrid, it overwrites the DataGrid's AllowSorting setting.

If I then set DataGridTableStyle.AllowSorting = False, it reverts back my sorting on DataTable for some reason. This behavior is undocumented and not mentioned anywhere on the web. The AllowSorting from DataGrid doesn't behave this way, so why the silliness? How do I get around it?

Try this one if it works:
Reset your datagrid into its original settings then make the sorting on your Query like :

"SELECT username, password FROM your datatable ORDER BY username asc"

Bind it on your datagrid using your TableStyle

In the column properties of youf grid set sortmode = notsortable. This will disable sorting when the user clicks on the column header

This article has been dead for over six months. Start a new discussion instead.