none
The CellBeginEdit event in DataGridView RRS feed

  • Question

  • There are 4 columns in DataGridView, the first colmun is CheckBox type, the code is below.

    ////////////////////////////////////////////////////////////////////////////

       Private Sub DataGridViewX1_CellBeginEdit(sender As Object, e As DataGridViewCellCancelEventArgs) Handles DataGridViewX1.CellBeginEdit
            If e.ColumnIndex = 0 AndAlso e.RowIndex <> -1 Then
                MessageBox.Show("Begin to edit")
            End If
        End Sub

        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load

            DataGridViewX1.Rows.Add(New Object() {True, "qq", "ccc", "vvv"})
            DataGridViewX1.Rows.Add(New Object() {False, "qq", "ccc", "vvv"})
            DataGridViewX1.Rows.Add(New Object() {False, "qq", "ccc", "vvv"})

        End Sub

    //////////////////////////////////////////////////////////////////////////////////////////////////

    But just start the program, click anywhere in DataGridView first, the dialog box (Begin to edit) will be shown? i only want to click the CheckBox to raise the event! subsequent clickings will be normal, why? thank you.


    • Edited by gaxjyxq Saturday, September 30, 2017 10:46 AM
    Saturday, September 30, 2017 10:45 AM

Answers

  •  I am not sure but,  maybe this will help.

    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DataGridViewX1.Rows.Add(New Object() {True, "qq", "ccc", "vvv"})
            DataGridViewX1.Rows.Add(New Object() {False, "qq", "ccc", "vvv"})
            DataGridViewX1.Rows.Add(New Object() {False, "qq", "ccc", "vvv"})
            DataGridViewX1.CurrentCell = Nothing
        End Sub
    
        Private Sub DataGridViewX1_CellBeginEdit(sender As Object, e As DataGridViewCellCancelEventArgs) Handles DataGridViewX1.CellBeginEdit
            If e.ColumnIndex = 0 AndAlso e.RowIndex > -1 Then
                MessageBox.Show("Begin to edit")
            End If
        End Sub
    
    End Class
    


    If you say it can`t be done then i`ll try it

    • Marked as answer by gaxjyxq Saturday, September 30, 2017 2:15 PM
    Saturday, September 30, 2017 2:01 PM
  • Hi gaxjyxq,

    According to your description, you want to pervert user from uncheck the checkbox, I do one example that you can refer to:

     Private Sub Form7_Load(sender As Object, e As EventArgs) Handles MyBase.Load
           
            DataGridView1.ColumnCount = 3
            DataGridView1.Columns(0).Name = "Product ID"
            DataGridView1.Columns(1).Name = "Product Name"
            DataGridView1.Columns(2).Name = "Product_Price"
    
            Dim row As String() = New String() {"1", "Product 1", "1000"}
            DataGridView1.Rows.Add(row)
            row = New String() {"2", "Product 2", "2000"}
            DataGridView1.Rows.Add(row)
            row = New String() {"3", "Product 3", "3000"}
            DataGridView1.Rows.Add(row)
            row = New String() {"4", "Product 4", "4000"}
            DataGridView1.Rows.Add(row)
    
            Dim chk As New DataGridViewCheckBoxColumn()
            DataGridView1.Columns.Add(chk)
            chk.HeaderText = "Check Data"
            chk.Name = "chk"
            DataGridView1.Rows(2).Cells(3).Value = True
    
    
        End Sub
    
        Private Sub DataGridView1_CellBeginEdit(sender As Object, e As DataGridViewCellCancelEventArgs) Handles DataGridView1.CellBeginEdit
            If e.ColumnIndex = 3 Then
                Dim row As DataGridViewRow = DataGridView1.Rows(e.RowIndex)
                If CBool(row.Cells(3).Value) = True Then
                    e.Cancel = True
                End If
            End If
        End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by gaxjyxq Tuesday, October 10, 2017 3:25 AM
    Monday, October 9, 2017 8:39 AM
    Moderator

All replies

  • You could work with CurrentCellDirtyStateChanged event e.g.

    Private Sub DataGridView1SelectAll_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged
        If TypeOf DataGridView1.CurrentCell Is DataGridViewCheckBoxCell Then
            ' 
        End If
    End Sub

    Or CellValueChanged event

    Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
        If DataGridView1.CurrentRow IsNot Nothing Then
            If e.ColumnIndex = DataGridView1.Columns("Your colum name goes here").Index  Then
    
            End If
        End If
    End Sub

    It all depends on what you are after or use

    Public Class Form1
        Private Sub DataGridViewX1_CellBeginEdit(sender As Object, e As DataGridViewCellCancelEventArgs)
            'Handles DataGridViewX1.CellBeginEdit
    
            If e.ColumnIndex = 0 AndAlso e.RowIndex <> -1 Then
                MessageBox.Show("Begin to edit")
            End If
    
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DataGridViewX1.Rows.Add(New Object() {True, "qq", "ccc", "vvv"})
            DataGridViewX1.Rows.Add(New Object() {False, "qq", "ccc", "vvv"})
            DataGridViewX1.Rows.Add(New Object() {False, "qq", "ccc", "vvv"})
        End Sub
    
        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
            AddHandler DataGridViewX1.CellBeginEdit, AddressOf DataGridViewX1_CellBeginEdit
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites



    Saturday, September 30, 2017 11:45 AM
    Moderator
  • Sorry, i need to use the CellBeginEdit because when user ticking the checkbox, i need to detect the condition, and prevent from ticking using the e.cancel if the condition is false, thank you.
    • Edited by gaxjyxq Saturday, September 30, 2017 1:15 PM
    Saturday, September 30, 2017 1:15 PM
  • Sorry, i need to use the CellBeginEdit because when user ticking the checkbox, i need to detect the condition, and prevent from ticking using the e.cancel if the condition is false, thank you.
    Did you try my code sample for CellBeginEdit (last one in my reply) ???

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, September 30, 2017 1:24 PM
    Moderator
  • The Addhandler? yes, i tried, the same result, click anywhere in DataGridView first time, the dialog box will popup.
    Saturday, September 30, 2017 1:48 PM
  • I need to keep a checkbox at least ticked when user untick the checkboxes, so if the last one checked checkbox will be unticked, i will prevent from unticking using the e.cancel=true in the CellBeginEdit event. But just start the program, click first time anywhere in DataGridView, the CellBeginEdit will be triggered.
    • Edited by gaxjyxq Saturday, September 30, 2017 1:57 PM
    Saturday, September 30, 2017 1:53 PM
  •  I am not sure but,  maybe this will help.

    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DataGridViewX1.Rows.Add(New Object() {True, "qq", "ccc", "vvv"})
            DataGridViewX1.Rows.Add(New Object() {False, "qq", "ccc", "vvv"})
            DataGridViewX1.Rows.Add(New Object() {False, "qq", "ccc", "vvv"})
            DataGridViewX1.CurrentCell = Nothing
        End Sub
    
        Private Sub DataGridViewX1_CellBeginEdit(sender As Object, e As DataGridViewCellCancelEventArgs) Handles DataGridViewX1.CellBeginEdit
            If e.ColumnIndex = 0 AndAlso e.RowIndex > -1 Then
                MessageBox.Show("Begin to edit")
            End If
        End Sub
    
    End Class
    


    If you say it can`t be done then i`ll try it

    • Marked as answer by gaxjyxq Saturday, September 30, 2017 2:15 PM
    Saturday, September 30, 2017 2:01 PM
  • Yes, add the "DataGridViewX1.CurrentCell = Nothing" to solve my problem, thank you very much.
    Saturday, September 30, 2017 2:14 PM
  • Another problem: I want to prevent user from unticking the checkboxes, only allow to tick the checkboxes, the code below will not work?

      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DataGridViewX1.Rows.Add(New Object() {True, "55", "ccc", "vvv"})
            DataGridViewX1.Rows.Add(New Object() {False, "66", "kkk", "vvv"})
            DataGridViewX1.Rows.Add(New Object() {False, "77", "mmm", "vvv"})
            DataGridViewX1.CurrentCell = Nothing

        End Sub

        Private Sub DataGridViewX1_CellBeginEdit(sender As Object, e As DataGridViewCellCancelEventArgs) Handles DataGridViewX1.CellBeginEdit
            If e.ColumnIndex = 0 Then
                Dim row As DataGridViewRow = DataGridViewX1.Rows(e.RowIndex)
                If CBool(row.Cells(0).Value) = True Then 
                    e.Cancel = True
                End If
            End If
        End Sub

    Wednesday, October 4, 2017 2:13 PM
  • Hi gaxjyxq,

    According to your description, you want to pervert user from uncheck the checkbox, I do one example that you can refer to:

     Private Sub Form7_Load(sender As Object, e As EventArgs) Handles MyBase.Load
           
            DataGridView1.ColumnCount = 3
            DataGridView1.Columns(0).Name = "Product ID"
            DataGridView1.Columns(1).Name = "Product Name"
            DataGridView1.Columns(2).Name = "Product_Price"
    
            Dim row As String() = New String() {"1", "Product 1", "1000"}
            DataGridView1.Rows.Add(row)
            row = New String() {"2", "Product 2", "2000"}
            DataGridView1.Rows.Add(row)
            row = New String() {"3", "Product 3", "3000"}
            DataGridView1.Rows.Add(row)
            row = New String() {"4", "Product 4", "4000"}
            DataGridView1.Rows.Add(row)
    
            Dim chk As New DataGridViewCheckBoxColumn()
            DataGridView1.Columns.Add(chk)
            chk.HeaderText = "Check Data"
            chk.Name = "chk"
            DataGridView1.Rows(2).Cells(3).Value = True
    
    
        End Sub
    
        Private Sub DataGridView1_CellBeginEdit(sender As Object, e As DataGridViewCellCancelEventArgs) Handles DataGridView1.CellBeginEdit
            If e.ColumnIndex = 3 Then
                Dim row As DataGridViewRow = DataGridView1.Rows(e.RowIndex)
                If CBool(row.Cells(3).Value) = True Then
                    e.Cancel = True
                End If
            End If
        End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by gaxjyxq Tuesday, October 10, 2017 3:25 AM
    Monday, October 9, 2017 8:39 AM
    Moderator