Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / February 2008

Tip: Looking for answers? Try searching our database.

Sort DataGridView on unbound column issue...

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Frederik - 15 Feb 2008 23:07 GMT
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
RobinS - 16 Feb 2008 07:30 GMT
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.
----------------------------------
> Hi group,
>
[quoted text clipped - 4 lines]
> Thanks (again) for your time,
> Frederik
Andrus - 18 Feb 2008 14:32 GMT
> 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.
RobinS - 23 Feb 2008 19:15 GMT
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.
-------------------------------
>> 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,
[quoted text clipped - 4 lines]
>
> Andrus.
Andrus - 24 Feb 2008 14:40 GMT
> 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);
}
RobinS - 24 Feb 2008 18:33 GMT
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.
-------------------------------
>> Or capture the CellClick event, and check the rowindex in the
>> DataGridViewCellEventArgs. If it's -1, they clicked on the header.
[quoted text clipped - 58 lines]
> base.OnColumnHeaderMouseClick(e);
> }
Andrus - 24 Feb 2008 23:30 GMT
>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.
RobinS - 25 Feb 2008 00:39 GMT
> >I haven't worked with virtual DGV's.
>
[quoted text clipped - 10 lines]
>
> 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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.