Quickie: DGV Sorting version Binding Source

JaedenRuiner

Well-known member
Joined
Aug 13, 2007
Messages
340
Programming Experience
10+
When You set the DataSource for a DataGridView to a BindingSource, I am wondering the intricacies of how it handles autosorting. Basically, (and i think it is a setting in the DataGridColumn classes, but since the current DGV is autogenerating columns, i'm not positive) when you click on the Column Header for the DGV it will sort Ascending/Descending respectively (with the nice cute little arrow graphic to discern the sort direction).

Does that use the BindingSource, or is it handled internally?
I want to enable "Multiple Column Sorting", via say CTRL+Click, so if a column header is clicked, and then I press CTRL and Click another column, it goes in order of clicks for the Sort order.
SQL: Order By Column1 ASC, Column2 ASC, Column3 DESC
or something to that effect. The Handling of the clicks and columns directions i can do, but I'm wondering if the DGV can handle multi-column Sorting or if I have to move it out to the bindingsource, and subsequently, if I must sort via the binding source, will that automatically refresh the bound DGV or do i need to refresh the DGV too in order to make the sorting affects display.

Thanks
 
When you click a column header it will set the Sort property of the BindingSource, which, in turn, sets the Sort property of the bound DataTable's DefaultView. As long as the list you bind to the BindingSource implements the IBindingList interface you can sort by clicking a grid column header. That's because Sort is a member of the IBindingList interface. If you bind a list to the BindingSource that doesn't implement that interface, like an array or simple List, then sorting will not occur automatically.
 
Fair enough. I somewhat expected that. But the real issue now is when does it occur?

We have _ColumnHeaderMouseClick() event method that would allow me to listen to the HeaderClick and affect the sorting my own way...
<edit>

This part works, by affecting the BindingSource.Sort property directly from ColumnHeaderClick works, however, the sorting glyph naturally still obeys the "SortedColumn" property of the DGV.

When I looked at the Column "SortMode" property it has programmatic which allows space for the "Sorting Glyph".....so where is it.

There is no ImageList property for the DGV, no, IconIndex or nothing for the DataGridColumn, so do I have to paint the sorting glyph manually?!?


Thanks
 
Last edited:
Er, isn't SortedColumn just something you use for drawing the glyph, if youre sorting programmatically?

i.e. if your grid is set to pgm, you set a sort of the bindsource, and then tell the dgv the SortedColumn so it will (only) draw the glyph, as a nicety to the user
 
Ah, no, didn't explain fully. I'm doing a "Multi-Column" Sort, so i would like the glyph to be drawn on Each column that is involved in the sorting.

when looking at the ListView, you can create (in details or report mode) subitems and subitem column headers, which allow you to select a "imageindex" from the ListView's "ImageList" to show that glyph for sorting purposes on the column headers themselves. (I know because I've done it).

However, with the DGV there is no "ImageList" or what not for the selection of such glyphs, so how would I "Draw" the gliph on the 'multiple' column headers to indicate sorting and sort direction.

thanks
 
Nps.. sometimes, all it needs is a different person to google words that "feel natural" to them ;)
 

Latest posts

Back
Top