Click here to Skip to main content
16,020,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Sir,

I need help on the below code which i used in datagridview cell click event.

VB
Private Sub Dgv_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Dgv.CellClick
        If e.RowIndex >= 0 Then
            Try
                Dim obj As New ConStr
                Dim sql As String
                Dim tbl As New DataTable
                Txt_Id.Text = Dgv.Rows(e.RowIndex).Cells(0).Value
                sql = "Select * From AA Where Code='" & Txt_Id.Text & "'"
                tbl = obj.GetRecords(sql)
                Txt_Name.Text = tbl.Rows(0)("Nm")
            Catch ex As IndexOutOfRangeException
                MsgBox(ex.Message, MsgBoxStyle.Critical)
            End Try
        End If
    End Sub


I have a Button in my form, I want when i click the Button, it will call the
above Dgv_CellClick event with the code i mention there.

What i will do for this proccess.

Rgds
Indranil
Posted
Updated 27-Mar-13 2:55am
v2
Comments
Leo Chapiro 27-Mar-13 8:59am    
Simply call the event handler method directly, what is the problem?

1 solution

Steps to do:
1) Select button
2) Go to the Properties window (right-bottom corner)
3) Choose events tab
4) Find "Click" event
5) Choose "DGV_CellClick" event

That's all!


[EDIT]
Use DataGridView.CurrentRow[^] property and "export" the body of procedure Dgv_CellClick into another one (for example UpdateText).

VB
Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
    UpdateText()
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    UpdateText()
End Sub

Private Sub UpdateText()
    Dim cr As DataGridViewRow = Nothing
    Try
        cr = Me.DataGridView1.CurrentRow
       'get data
       'update text
        Me.Txt_Name.Text = "new text"

    Catch ex As Exception
        MsgBox(Err.Description, MsgBoxStyle.Exclamation, "Error")
    Finally
        cr = Nothing
    End Try

End Sub

[\EDIT]
 
Share this answer
 
v2
Comments
hspl 27-Mar-13 10:25am    
Sir, "Dgv_CellClick" option is not showing in Click event dropdownlist of Button event tab option.
Maciej Los 27-Mar-13 10:47am    
Sorry...
See updated solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900