By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,627 Members | 1,188 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,627 IT Pros & Developers. It's quick & easy.

How do I disable a button if First Row of DataGridView selected ?

P: 5
Hi all,

I want be able to enable and disable navigation buttons when on the first or
last row of a DataGridView object.

I have a populated DataGridView and two buttons - "First Row" and "Previous Row".
If the user clicks "First Row", they are taken to the first row in the DataGridView via:

Expand|Select|Wrap|Line Numbers
  1. Private Sub btnFirstRecord_Click(...) 
  2. ' Move to first record in job list 
  3. dgvMyRecords.CurrentCell = dgvMyRecords.Rows(0).Cells(0)
  4. End Sub
What I then want to to is disable the Prev Row button like this...

Expand|Select|Wrap|Line Numbers
  1. Private Sub dgvMyRecords_SelectionChanged(...)
  2. If dgvMyRecords.CurrentRow.Index = 0 Then 
  3. btnPrevRow.Enabled = False
  4. Else 
  5. btnPrevRow.Enabled = True 
  6. End if 
  7. End Sub
The problem is... this works if the user selects the first record using the mouse,
but not if the first record is selected through the code using CurrentCell.

If I click on the first row, SelectionChanged gets called AFTER the row gets changed.
If I set dgvMyRecords.CurrentCell to dgvMyRecords.Rows(0).Cells(0) (ie. select the first row via code), SelectionChanged gets called, but BEFORE the row gets changed !?!?!?

Can anyone show me how to do this ?

cheers,
Matt.
Jul 9 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.