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 / Windows Forms / WinForm Controls / June 2008

Tip: Looking for answers? Try searching our database.

DataGridView col header click triggers SelectionChanged

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
John A Grandy - 28 Jun 2008 01:14 GMT
Apparently clicking on a DataGridView column header triggers the
SelectionChanged event ...

I'm not sure why this is happenning ... possibilities I can think of are :

1. the selection actually changes to the column header ( ? this seems
unlikely and would be a poor implementation in my opinion )

2. a column sort is triggerred, which the grid interprets as changing the
selection

3. some other reason

In any event, the problem for my app is that when a col header is clicked
and SelectionChanged fires, CurrentRow is undefined on entry to my event
handler.  Not sure why this would be ( unless because of option 1 above ).

Can anyone shed any light on all of this ?   Thanks.
Jack Jackson - 28 Jun 2008 21:18 GMT
>Apparently clicking on a DataGridView column header triggers the
>SelectionChanged event ...
[quoted text clipped - 14 lines]
>
>Can anyone shed any light on all of this ?   Thanks.

I have just spent a lot of time the last couple of days investigating
row selection in the DataGridView, and this is what I have learned.

The problems with column sorting are actually worse than you think.

When the grid is sorted, the grid destroys and recreates all of the
DataGridViewRow objects.  SelectionChanged fires twice, the first time
with no rows selected (presumably when all of the row objects are
destroyed) and the second time with one row selected (even if more
than one row was selected before the sort, only one will be selected
after).

After sorting most likely a different row will be selected than
before.  If the 3rd row was selected before, the 3rd row will be
selected after, which most likely will be a different row.

What I have done to get around some of these problems is to build a
list of selected rows in the OnCellClick event when the row is -1
(column header row clicked) and the column's SortMode is Automatic.

As long as the list exists, I ignore SelectionChanged events (if there
were rows selected before the sort, there should be two of these
events).

In the Sorted event (which fires after the sort is complete), if the
list exists, I call ClearSelection and then select the rows in the
list, then remove the list.

The only problem with this approach is determining which rows after
the sort match the rows that were selected before the sort.  I ended
up remembering the concatenation of Cell.Value.ToString +
Environment.NewLine for each cell in each selected row.  It isn't
perfect, but as long as no two rows have the same values in all cells
it works.  I wanted to implement a generic way to handle this in my
subclass of DataGridView, and without knowing details of the data
source I couldn't figure out any other way of matching the rows.

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.