Sort DataGridView on unbound column issue...

F

Frederik

Hi group,

I have a DataGridView that is mainly consisting of data bound columns.
One column however is unbound. I'm not able to sort the grid on this
column! Is that normal behaviour? If so, what are my options?

Thanks (again) for your time,
Frederik
 
R

RobinS

Yes, it is normal behavior. If the column is not databound, it doesn't know
the underlying data source to handle the sorting.

If you are using datasets or data tables, you could add a column to your
dataset.

Otherwise, you can try handling the sort stuff yourself. You could try
capturing the column header click event, check to see what column it is, and
sort it yourself.

I just created a dgv bound to a BindingList<myObj>, and found excellent
examples for handling the sorting in Brian Noyes' Data Binding book.

RobinS.
GoldMail, Inc.
 
A

Andrus

Otherwise, you can try handling the sort stuff yourself. You could try
capturing the column header click event, check to see what column it is,
and sort it yourself.

How to capture mouse click in grid column header ?
Grid OnMouseClick not not have such flag.

Andrus.
 
R

RobinS

Capture the ColumnHeaderMouseClick event.

Or capture the CellClick event, and check the rowindex in the
DataGridViewCellEventArgs. If it's -1, they clicked on the header.

RobinS.
GoldMail, Inc.
-------------------------------
 
A

Andrus

Or capture the CellClick event, and check the rowindex in the
DataGridViewCellEventArgs. If it's -1, they clicked on the header.

RobinS,

Thank you. I tried MSDN sample below with Virtual Mode DataGridView. Issues:

1. Sort glyph is not visible in most columns. It is located too far right.
It is visible only if I make
column very wide. How to force sorh glyph to appear immediately after column
header text or before header ? Or is it possible to use some other sort
indicator ?

2. Sort(newColumn, direction);

causes exception
operation not supported when grid is in virtual mode. I commented it out for
testing.

3. If new column is sorted, sort glyph is not removed from old column.

4. Descending sort order glyph is never displayed.

How to fix those issues ?

Andrus.

protected override void
OnColumnHeaderMouseClick(DataGridViewCellMouseEventArgs e) {
DataGridViewColumn newColumn = Columns[e.ColumnIndex];
DataGridViewColumn oldColumn = SortedColumn;
ListSortDirection direction;

// If oldColumn is null, then the DataGridView is not sorted.
if (oldColumn != null) {
// Sort the same column again, reversing the SortOrder.
if (oldColumn == newColumn && SortOrder == SortOrder.Ascending) {
direction = ListSortDirection.Descending;
} else {

// Sort a new column and remove the old SortGlyph.
direction = ListSortDirection.Ascending;
oldColumn.HeaderCell.SortGlyphDirection = SortOrder.None;
}

} else {
direction = ListSortDirection.Ascending;
}

// Sort the selected column.
// this causes exception: operation not supported when grid is in virtual
mode:
//Sort(newColumn, direction);

newColumn.HeaderCell.SortGlyphDirection =
direction == ListSortDirection.Ascending ?
SortOrder.Ascending : SortOrder.Descending;
base.OnColumnHeaderMouseClick(e);
}
 
R

RobinS

I haven't worked with virtual DGV's. My guess would be that to sort one, you
would have to sort the underlying data source. Are you binding to a
BindingSource, and have you tried sorting using that rather than sorting the
grid itself?

RobinS.
GoldMail, Inc.
-------------------------------
Andrus said:
Or capture the CellClick event, and check the rowindex in the
DataGridViewCellEventArgs. If it's -1, they clicked on the header.

RobinS,

Thank you. I tried MSDN sample below with Virtual Mode DataGridView.
Issues:

1. Sort glyph is not visible in most columns. It is located too far right.
It is visible only if I make
column very wide. How to force sorh glyph to appear immediately after
column
header text or before header ? Or is it possible to use some other sort
indicator ?

2. Sort(newColumn, direction);

causes exception
operation not supported when grid is in virtual mode. I commented it out
for
testing.

3. If new column is sorted, sort glyph is not removed from old column.

4. Descending sort order glyph is never displayed.

How to fix those issues ?

Andrus.

protected override void
OnColumnHeaderMouseClick(DataGridViewCellMouseEventArgs e) {
DataGridViewColumn newColumn = Columns[e.ColumnIndex];
DataGridViewColumn oldColumn = SortedColumn;
ListSortDirection direction;

// If oldColumn is null, then the DataGridView is not sorted.
if (oldColumn != null) {
// Sort the same column again, reversing the SortOrder.
if (oldColumn == newColumn && SortOrder == SortOrder.Ascending) {
direction = ListSortDirection.Descending;
} else {

// Sort a new column and remove the old SortGlyph.
direction = ListSortDirection.Ascending;
oldColumn.HeaderCell.SortGlyphDirection = SortOrder.None;
}

} else {
direction = ListSortDirection.Ascending;
}

// Sort the selected column.
// this causes exception: operation not supported when grid is in virtual
mode:
//Sort(newColumn, direction);

newColumn.HeaderCell.SortGlyphDirection =
direction == ListSortDirection.Ascending ?
SortOrder.Ascending : SortOrder.Descending;
base.OnColumnHeaderMouseClick(e);
}
 
A

Andrus

I haven't worked with virtual DGV's.

How about the sort hlyph display issues ?
I think they apper in normal dgv also.
My guess would be that to sort one, you would have to sort the underlying
data source. Are you binding to a BindingSource, and have you tried
sorting using that rather than sorting the grid itself?

It is not possible to use binding in VirtualMode.
ORDER BY clause of base query must generated dynamically in this case.
I'm looking for such sample.

Andrus.
 
R

RobinS

Andrus said:
How about the sort hlyph display issues ?
I think they apper in normal dgv also.


It is not possible to use binding in VirtualMode.
ORDER BY clause of base query must generated dynamically in this case.
I'm looking for such sample.

Andrus.

I haven't had any problems with the sort glyph in my DGVs that are bound to
either datasets or customer business objects.

You really might want to check out the data binding book by Brian Noyes; he
does discuss virtual DGV mode in that book.

Also, have you seen the DataGridView FAQ? Try googling for that; it's a 70+
page document that I found helpful.

RobinS.
GoldMail, Inc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top