call CellContentDoubleClick event from button outside DataGridView?

rajdh75

Active member
Joined
Mar 30, 2020
Messages
26
Programming Experience
Beginner
Hellow,
I have code for DataGridView1_CellContentDoubleClick to edit row from another form as
VB.NET:
 Private Sub DataGridView1_CellContentDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentDoubleClick
        Dim cells = DataGridView1.Rows(e.RowIndex).Cells
        Using dialogue As New FrmAddItemsNew
            With dialogue
                .TxtTestID.Text = cells(0).Value.ToString()
                .TxtInvoiceID.Text = cells(1).Value.ToString()
                .TxtProductID.Text = cells(2).Value.ToString()
                .CmbProductName.Text = CStr(cells(3).Value)
                .TxtHSN.Text = cells(4).Value.ToString()
                .TxtSerialNo.Text = cells(5).Value.ToString()
                .TxtUnit.Text = cells(6).Value.ToString()
                .TxtQty.Text = cells(7).Value.ToString()
                .TxtInclRate.Text = cells(8).Value.ToString()
                .TxtBaseRate.Text = cells(9).Value.ToString()
                .TxtAmount.Text = cells(10).Value.ToString()
                .TxtGST.Text = cells(11).Value.ToString()
                .TxtGSTAmt.Text = cells(12).Value.ToString()
                .TxtTotal.Text = cells(13).Value.ToString()

                .BtnEdit.Visible = True
                '.BtnEdit.Enabled = False

                If .ShowDialog() = DialogResult.OK Then
                    cells(0).Value = .TxtTestID.Text
                    cells(1).Value = .TxtInvoiceID.Text
                    cells(2).Value = .TxtProductID.Text
                    cells(3).Value = .CmbProductName.Text
                    cells(4).Value = .TxtHSN.Text
                    cells(5).Value = .TxtSerialNo.Text
                    cells(6).Value = .TxtUnit.Text
                    cells(7).Value = .TxtQty.Text
                    cells(8).Value = .TxtInclRate.Text
                    cells(9).Value = .TxtBaseRate.Text
                    cells(10).Value = .TxtAmount.Text
                    cells(11).Value = .TxtGST.Text
                    cells(12).Value = .TxtGSTAmt.Text
                    cells(13).Value = .TxtTotal.Text
                End If
            End With
        End Using
    End Sub
It works well.
Now if I want to execute this code on Button Click named as BtnEdit witch is outside of datagridview.
when I copied this code to BtnEdit it gives error to following line -
Dim cells = DataGridView1.Rows(e.RowIndex).Cells
'e' is not declared. It may be inaccessible due to its protection level.
How to solve this error ?

Thanks in advance.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,376
Location
Sydney, Australia
Programming Experience
10+
You don't call an event. Events are raised when things happen. The CellContentDoubleClick event is raised when the user double-clicks the contents of a cell. That's it, that's all.

If you want the same code executed under two different sets of circumstances, i.e. when two different events are raised, then you have two choices:
  1. Add both events to the Handles clause of the same method and put your code in that method.
  2. Put your code in its own method and then call that method from two different event handlers.
The first option is only available if the e parameter is the same for both events of you don't use that parameter in the method and can declare it as the common EventArgs type. It's also only an option if you want exactly the same code executed on both events. That means that it's not an option for you.

What you need to do is write your own method that accepts either the grid row itself or its index as a parameter and then uses that row. You can then call that method from the two event handlers. I would suggest your new method should look like this:
VB.NET:
Private Sub EditRow(row As DataGridViewRow)
    Dim cell = row.Cells
    
    'Etc'
End Sub
You would obviously copy the rest of the code you already have into there too. The event handler you currently have then becomes this:
VB.NET:
Private Sub DataGridView1_CellContentDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentDoubleClick
    EditRow(DataGridView1.Rows(e.RowIndex))
End Sub
and your new event handler would look like this:
VB.NET:
Private Sub BtnEdit_Click(sender As Object, e As EventArgs) Handles BtnEdit.Click
    EditRow(DataGridView1.CurrentRow)
End Sub
This is how you need to think when writing code: methods are chunks of functionality that can be invoked from wherever they are needed.
 

rajdh75

Active member
Joined
Mar 30, 2020
Messages
26
Programming Experience
Beginner
Thanks

jmcilhinney

I applied your code for EditRow() as -
EditRow:
 Private Sub EditRow(row As DataGridViewRow)
        Dim cell = row.Cells
        Using dialogue As New FrmAddItemsNew

            With dialogue
                .TxtTestID.Text = cell(0).Value.ToString()
                .TxtInvoiceID.Text = cell(1).Value.ToString()
                .TxtProductID.Text = cell(2).Value.ToString()
                .CmbProductName.Text = CStr(cell(3).Value)
                .TxtHSN.Text = cell(4).Value.ToString()
                .TxtSerialNo.Text = cell(5).Value.ToString()
                .TxtUnit.Text = cell(6).Value.ToString()
                .TxtQty.Text = cell(7).Value.ToString()
                .TxtInclRate.Text = cell(8).Value.ToString()
                .TxtBaseRate.Text = cell(9).Value.ToString()
                .TxtAmount.Text = cell(10).Value.ToString()
                .TxtGST.Text = cell(11).Value.ToString()
                .TxtGSTAmt.Text = cell(12).Value.ToString()
                .TxtTotal.Text = cell(13).Value.ToString()

                .BtnEdit.Visible = True
                '.BtnEdit.Enabled = False

                If .ShowDialog() = DialogResult.OK Then
                    cell(0).Value = .TxtTestID.Text
                    cell(1).Value = .TxtInvoiceID.Text
                    cell(2).Value = .TxtProductID.Text
                    cell(3).Value = .CmbProductName.Text
                    cell(4).Value = .TxtHSN.Text
                    cell(5).Value = .TxtSerialNo.Text
                    cell(6).Value = .TxtUnit.Text
                    cell(7).Value = .TxtQty.Text
                    cell(8).Value = .TxtInclRate.Text
                    cell(9).Value = .TxtBaseRate.Text
                    cell(10).Value = .TxtAmount.Text
                    cell(11).Value = .TxtGST.Text
                    cell(12).Value = .TxtGSTAmt.Text
                    cell(13).Value = .TxtTotal.Text

                End If
            End With
        End Using

    End Sub
For DataGridView1_CellContentDoubleClick as -
DataGridView1_CellContentDoubleClick:
Private Sub DataGridView1_CellContentDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentDoubleClick
        EditRow(DataGridView1.Rows(e.RowIndex))
End Sub
For BtnEdit_Click as -
BtnEdit_Click:
 Private Sub BtnEdit_Click(sender As Object, e As EventArgs) Handles BtnEdit.Click
        EditRow(DataGridView1.CurrentRow)

    End Sub
Code for DataGridView1_CellContentDoubleClick works well when I Double click rows and edit. It works and database is updated for multiple rows but when I want to use this code by BtnEdit it updates multiple rows in datagridview1, but saves only first edited row to database. It doesnot save other updated rows in database.
My Save code is as -
BtnSave_Click:
Private Sub BtnSave_Click(sender As Object, e As EventArgs) Handles BtnSave.Click
        Me.Validate()
        Me.myDA.Update(Me.myDataSet.Tables("TblTest"))
        Me.myDataSet.AcceptChanges()
        
        MessageBox.Show("Invoice Updated.")
        End If
        Me.Close()

    End Sub
Is anything wrong in BtnEdit code ?
Thanks once again
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,376
Location
Sydney, Australia
Programming Experience
10+
Debug your code. Set a breakpoint and step through the code to see what it actually does and whether that's what you expect. It's the same EditRow method in both cases so it has to work the same in both cases. If I had to guess - which I do - I'd say that the CurrentRow of the grid is not row you think it is when you click the Button. That's the sort of stuff you use the debugger to test and that should be done before posting here. If you don't know how to debug, stop what you're doing and learn now because it's something all developers must be able to do.
 
Top Bottom