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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. disable selection and context menu for a single column in datagrid view

disable selection and context menu for a single column in datagrid view

Scheduled Pinned Locked Moved Visual Basic
tutorialquestion
8 Posts 2 Posters 1 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.
  • S Offline
    S Offline
    sohaib_a
    wrote on last edited by
    #1

    I have a datagrid view with two columns.There is a context menu which should be disabled when i click on any cell in the second column and also selection should be disabled. I tried to do this by setting context menu properties for each column accordingly but the menu still appears. And how to disable select for the the same column?

    D 1 Reply Last reply
    0
    • S sohaib_a

      I have a datagrid view with two columns.There is a context menu which should be disabled when i click on any cell in the second column and also selection should be disabled. I tried to do this by setting context menu properties for each column accordingly but the menu still appears. And how to disable select for the the same column?

      D Offline
      D Offline
      dan sh
      wrote on last edited by
      #2

      On MouseDown, check the column where mouse is clicked. This can be done using HitTest method. To disable selection of cells in a particular column, you will need to override SetSelectedColumnCore method.

      The word "politics" describes the process so well: "Poli" in Latin meaning "many" and "tics" meaning "bloodsucking creatures." जय हिंद

      S 1 Reply Last reply
      0
      • D dan sh

        On MouseDown, check the column where mouse is clicked. This can be done using HitTest method. To disable selection of cells in a particular column, you will need to override SetSelectedColumnCore method.

        The word "politics" describes the process so well: "Poli" in Latin meaning "many" and "tics" meaning "bloodsucking creatures." जय हिंद

        S Offline
        S Offline
        sohaib_a
        wrote on last edited by
        #3

        I tried the code below,but the result isnt as expected.

        Dim hti As DataGridView.HitTestInfo = DataGridView1.HitTest(e.X, e.Y)

            column1 = hti.ColumnIndex
            If e.Button = Windows.Forms.MouseButtons.Right Then
                If hti.ColumnIndex = 1 Then
        
                    DataGridView1.ContextMenuStrip = ContextMenuStrip1
                    DataGridView1.ContextMenuStrip.Enabled = False
                    DataGridView1.ContextMenuStrip.Visible = False
                    
        
        
                Else
                    DataGridView1.ContextMenuStrip = ContextMenuStrip1
        

        End If
        End If

        When I click on the second column the menu strip still appears but it appears disable.So the enabled=false is fine but why isn't the visible= false working?

        D 1 Reply Last reply
        0
        • S sohaib_a

          I tried the code below,but the result isnt as expected.

          Dim hti As DataGridView.HitTestInfo = DataGridView1.HitTest(e.X, e.Y)

              column1 = hti.ColumnIndex
              If e.Button = Windows.Forms.MouseButtons.Right Then
                  If hti.ColumnIndex = 1 Then
          
                      DataGridView1.ContextMenuStrip = ContextMenuStrip1
                      DataGridView1.ContextMenuStrip.Enabled = False
                      DataGridView1.ContextMenuStrip.Visible = False
                      
          
          
                  Else
                      DataGridView1.ContextMenuStrip = ContextMenuStrip1
          

          End If
          End If

          When I click on the second column the menu strip still appears but it appears disable.So the enabled=false is fine but why isn't the visible= false working?

          D Offline
          D Offline
          dan sh
          wrote on last edited by
          #4

          Instead of taking this much pain, just reinitialize the contextmenustrip for the second column. Change the following C# code to VB.

          this.dataGridView1.ContextMenuStrip = oContextMnuStrip;
          this.dataGridView1.Columns[1].ContextMenuStrip = new ContextMenuStrip();

          You will not need any extra code then.

          The word "politics" describes the process so well: "Poli" in Latin meaning "many" and "tics" meaning "bloodsucking creatures." जय हिंद

          S 1 Reply Last reply
          0
          • D dan sh

            Instead of taking this much pain, just reinitialize the contextmenustrip for the second column. Change the following C# code to VB.

            this.dataGridView1.ContextMenuStrip = oContextMnuStrip;
            this.dataGridView1.Columns[1].ContextMenuStrip = new ContextMenuStrip();

            You will not need any extra code then.

            The word "politics" describes the process so well: "Poli" in Latin meaning "many" and "tics" meaning "bloodsucking creatures." जय हिंद

            S Offline
            S Offline
            sohaib_a
            wrote on last edited by
            #5

            Ok..thanks alot.Regarding disabling the column selection where is SetSelectedColumnCore method found in vb.net?

            D 1 Reply Last reply
            0
            • S sohaib_a

              Ok..thanks alot.Regarding disabling the column selection where is SetSelectedColumnCore method found in vb.net?

              D Offline
              D Offline
              dan sh
              wrote on last edited by
              #6

              It is there in the DataGridView class. Since it is protected virtual method, you will need to inherit the DataGridView class in order to override.

              The word "politics" describes the process so well: "Poli" in Latin meaning "many" and "tics" meaning "bloodsucking creatures." जय हिंद

              S 1 Reply Last reply
              0
              • D dan sh

                It is there in the DataGridView class. Since it is protected virtual method, you will need to inherit the DataGridView class in order to override.

                The word "politics" describes the process so well: "Poli" in Latin meaning "many" and "tics" meaning "bloodsucking creatures." जय हिंद

                S Offline
                S Offline
                sohaib_a
                wrote on last edited by
                #7

                Ok..that sounds a little complicated for me,is there any other simpler way or do you have any code samples for it. Also after i select a cell and click on the context menu,the cell at 0,0 gets selected by default.I dont want any cell to get highlighted unless i click on it.How can this be done?

                D 1 Reply Last reply
                0
                • S sohaib_a

                  Ok..that sounds a little complicated for me,is there any other simpler way or do you have any code samples for it. Also after i select a cell and click on the context menu,the cell at 0,0 gets selected by default.I dont want any cell to get highlighted unless i click on it.How can this be done?

                  D Offline
                  D Offline
                  dan sh
                  wrote on last edited by
                  #8

                  Sohaib_A wrote:

                  Ok..that sounds a little complicated for me,is there any other simpler way or do you have any code samples for it.

                  Cant think of any other way.

                  Sohaib_A wrote:

                  Also after i select a cell and click on the context menu,the cell at 0,0 gets selected by default.I dont want any cell to get highlighted unless i click on it.How can this be done?

                  Setting focus might work out.

                  The word "politics" describes the process so well: "Poli" in Latin meaning "many" and "tics" meaning "bloodsucking creatures." जय हिंद

                  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