how do I disable buttons if first row selected in DataGridView?
-
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:
Private Sub btnFirstRecord_Click(...) ' Move to first record in job list dgvMyRecords.CurrentCell = dgvMyRecords.Rows(0).Cells(0) End Sub
What I then want to to is disable the Prev Row button like this...Private Sub dgvMyRecords_SelectionChanged(...) If dgvMyRecords.CurrentRow.Index = 0 Then btnPrevRow.Enabled = False Else btnPrevRow.Enabled = True End if 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 !?!?!? :confused: Can anyone show me how to do this ? cheers, Matt. -
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:
Private Sub btnFirstRecord_Click(...) ' Move to first record in job list dgvMyRecords.CurrentCell = dgvMyRecords.Rows(0).Cells(0) End Sub
What I then want to to is disable the Prev Row button like this...Private Sub dgvMyRecords_SelectionChanged(...) If dgvMyRecords.CurrentRow.Index = 0 Then btnPrevRow.Enabled = False Else btnPrevRow.Enabled = True End if 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 !?!?!? :confused: Can anyone show me how to do this ? cheers, Matt.Try using the DataGridView CellEnter event rather than the SelectionChanged event to toggle your navigation buttons.
-
Try using the DataGridView CellEnter event rather than the SelectionChanged event to toggle your navigation buttons.
Thanks Thomas... thats done the job nicely! Still have issues with the SelectionChanged behaviour though, but you've given me a workaround... cheers, Matt.