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?

Recommended Answers

All 3 Replies

sorry wrong thread.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.