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 General / January 2008

Tip: Looking for answers? Try searching our database.

Unslect a row in DataGridView

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Stephen Hatfield - 31 Jan 2008 19:36 GMT
I have a Windows Forms application consisting of several main level
TabControls and each of these controls has two or more TabControls within
them.

I have a method which calls all the LoadDataGridViews methods which I call
in the form's constructor. The last line in each method is
datagridview.ClearSelection().

These methods also get called in a TabControl_Selected event handler.

The first time I go into a given tab, all DataGridViews have the first row
selected. If I click out and click back into a given tab, the first rows are
now unselected.

I have also tried ClearSelection for each grid individually within a
Form_Load event with the method for loading all the grids within it as well.

Debugging shows the method is called either way yet the first row is always
selected the first time the grid is displayed.

Any suggestions would be most appreciated!

Thanks!
Stephen
Jack Jackson - 31 Jan 2008 20:00 GMT
>I have a Windows Forms application consisting of several main level
>TabControls and each of these controls has two or more TabControls within
[quoted text clipped - 17 lines]
>
>Any suggestions would be most appreciated!

My experience suggests that the first time a DataGridView is
displayed, either for the first time since it was created or when its
Visible property is changed from False to True, it will show the
current record as selected.

You need to arrange to call ClearSelection() after the grid has been
displayed.

I have had success with this code, which tracks the CurrentCellChanged
and VisibleChanged events and tries to determine when the selection
has been set due to the visibility changing:

   Private m_clearCurrentCell as Boolean = False
   Private m_lastVisible as Boolean = False

   Protected Overrides Sub OnCurrentCellChanged(ByVal e As
System.EventArgs)
       MyBase.OnCurrentCellChanged(e)

       ' If we have a CurrentCell, control is now visible, and
control was not
       ' visible the last time we got a VisibleChanged event, that
means that
       ' CurrentCell changed due to Visible changing from False to
True, so we
       ' set CurrentCell to Nothing.
       If CurrentCell IsNot Nothing AndAlso Visible AndAlso Not
m_lastVisible Then
           m_clearCurrentCell = True
       End If
   End Sub

   Protected Overrides Sub OnVisibleChanged(ByVal e As
System.EventArgs)
       MyBase.OnVisibleChanged(e)

       If Visible And m_clearCurrentCell Then
           ClearSelection()
       End If
       m_clearCurrentCell = False
       m_lastVisible = Visible
   End Sub

Rate this thread:







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.