Sorting DataGrid without GUI effects

Hello -

I have a DataGrid with many columns. I want to allow the user to sort the DataGrid based on a particular column. Vis Studio 2003 will allowing sorting by changing the DataGrid AllowSorting property to true.

Unfortunately, when you click on a column header, the Control places a really wide Triangle graphic in the header to indicate sorting (ASC or DESC). Since I have so many columns, I'd like my Column Widths to be just large enough to hold the text contents, but the sorting triangle image takes up quite a few pixels. Is there anyway to override the graphic displayed when sorting or to perform sorting without displaying the triangle graphic?

Thanks,
Nathan

[682 byte] By [nathankoterba] at [2007-12-16]
# 1
No - the DataGrid automatically puts the triangle there and there isn't a way to turn it off. The new DataGridView control does support this in Visual Studio 2005.

-mark
Program Manager
Microsoft
This post is provided "as-is"

MarkRideout at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 2
Thanks for the speedy response.

I assumed that was the case so created a datagrid that uses the minimum column widths (based on either the column data or column header text using measureString). Then, I used the MouseDown DataGrid event on a column header to dynamically resize all the columns to their minimum widths (to reset all the widths) and then resize the column header that was clicked on to sort so it's just large enough to display the sorting triangle graphic and the HeaderText.

With this code, the DataGrid columns are all minimum width but the one column being sorted. It works quite well and allows the entire DataGrid width to be displayed in my form.

I'm glad you did fix the triangle-sorting image issue in Vis 2005, because in 2003, the tiny triangle graphic takes up 42 pixels in width (which is two times or more than some of the widths of the columns in my DataGrid).

Nate

nathankoterba at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 3
Can you tell me how do you sort you columns ?
imdqa at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms General...