Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. how do I disable buttons if first row selected in DataGridView?

how do I disable buttons if first row selected in DataGridView?

Scheduled Pinned Locked Moved Visual Basic
questiondatabasehelptutorialcareer
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    matt_in_oz
    wrote on last edited by
    #1

    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.

    T 1 Reply Last reply
    0
    • M matt_in_oz

      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.

      T Offline
      T Offline
      Thomas Chester
      wrote on last edited by
      #2

      Try using the DataGridView CellEnter event rather than the SelectionChanged event to toggle your navigation buttons.

      M 1 Reply Last reply
      0
      • T Thomas Chester

        Try using the DataGridView CellEnter event rather than the SelectionChanged event to toggle your navigation buttons.

        M Offline
        M Offline
        matt_in_oz
        wrote on last edited by
        #3

        Thanks Thomas... thats done the job nicely! Still have issues with the SelectionChanged behaviour though, but you've given me a workaround... cheers, Matt.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups