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. Prevent DataGridView selecting a row - is it possible?

Prevent DataGridView selecting a row - is it possible?

Scheduled Pinned Locked Moved Visual Basic
algorithmshelpquestion
4 Posts 3 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.
  • N Offline
    N Offline
    nzmike
    wrote on last edited by
    #1

    Hi all, I have a sorted, non-editable DataGridView which contains some cells in each row that are traffic-lighted (ie: they have a different b/g colour depending on value) but when I populate the control the row that was the first one (before sorting) is always highlighted thus wiping out my custom coloured cells. I have tried every combo of colours and highlighting options to try and stop the DGV doing this but can't find a way. Ideally I just want to suppress altogether the DGV control selecting a row when it populates - does anyone know how I can go about this? This is driving me mad so I hope someone can help! TIA... Mike

    L 1 Reply Last reply
    0
    • N nzmike

      Hi all, I have a sorted, non-editable DataGridView which contains some cells in each row that are traffic-lighted (ie: they have a different b/g colour depending on value) but when I populate the control the row that was the first one (before sorting) is always highlighted thus wiping out my custom coloured cells. I have tried every combo of colours and highlighting options to try and stop the DGV doing this but can't find a way. Ideally I just want to suppress altogether the DGV control selecting a row when it populates - does anyone know how I can go about this? This is driving me mad so I hope someone can help! TIA... Mike

      L Offline
      L Offline
      Leah_Garrett
      wrote on last edited by
      #2

      There should be an option where readonly means "for use as display only no selection please". :) I got this effect in a recent project by deselecting everything just after the DataGridView was populated. The Deslection:

      Private Sub DeselectGridRows(ByVal dgv As DataGridView)
          Dim dgr As DataGridViewRow
          For Each dgr In dgv.SelectedRows
              dgr.Selected = False
          Next
      End Sub
      

      Deselecting called form within the forms load event:

      Private Sub Form1\_Load(ByVal sender As System.Object, 
                     ByVal e As System.EventArgs) Handles MyBase.Load
      
              Windows.Forms.Cursor.Current = Cursors.WaitCursor
              Me.SuspendLayout()
      
              PopulateDataGridView(dgv)
              DeselectGridRows(dgv)
      
      
              Me.ResumeLayout()
              Windows.Forms.Cursor.Current = Cursors.Default
          End If
      End Sub
      
      N 1 Reply Last reply
      0
      • L Leah_Garrett

        There should be an option where readonly means "for use as display only no selection please". :) I got this effect in a recent project by deselecting everything just after the DataGridView was populated. The Deslection:

        Private Sub DeselectGridRows(ByVal dgv As DataGridView)
            Dim dgr As DataGridViewRow
            For Each dgr In dgv.SelectedRows
                dgr.Selected = False
            Next
        End Sub
        

        Deselecting called form within the forms load event:

        Private Sub Form1\_Load(ByVal sender As System.Object, 
                       ByVal e As System.EventArgs) Handles MyBase.Load
        
                Windows.Forms.Cursor.Current = Cursors.WaitCursor
                Me.SuspendLayout()
        
                PopulateDataGridView(dgv)
                DeselectGridRows(dgv)
        
        
                Me.ResumeLayout()
                Windows.Forms.Cursor.Current = Cursors.Default
            End If
        End Sub
        
        N Offline
        N Offline
        nzmike
        wrote on last edited by
        #3

        Thanks Leah.... actually I'm not sure why I even posted the question as I realised soon after all you need to do is say datagridview1.SelectedRows(0).Selected = false and that takes care of it. What I want to do now is only highlight a row when the user clicks on one but rather than using a background colour (which obscures my cell traffic-lighting) I want to leave the B/G and F/G colours as they are and instead draw a 2-pixel red border around the entire row - and I'm thinking the only way to do that is to override one of the Paint methods... anyone know of a better/simpler way?

        D 1 Reply Last reply
        0
        • N nzmike

          Thanks Leah.... actually I'm not sure why I even posted the question as I realised soon after all you need to do is say datagridview1.SelectedRows(0).Selected = false and that takes care of it. What I want to do now is only highlight a row when the user clicks on one but rather than using a background colour (which obscures my cell traffic-lighting) I want to leave the B/G and F/G colours as they are and instead draw a 2-pixel red border around the entire row - and I'm thinking the only way to do that is to override one of the Paint methods... anyone know of a better/simpler way?

          D Offline
          D Offline
          DA_Loring
          wrote on last edited by
          #4

          This is proably an old thread now and past it's sell by date, but I did put an article in about drawing cell colours that may or may not help. Search the articles for 'Couluring and centering cells in datagridview' - Yes it contains a typo that got uploaded to the web!

          David Loring !! Keep Music Live !!

          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