且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在按钮单击中调用Datagridview单元格单击事件

更新时间:2023-02-20 16:21:54

要做的步骤:

1)选择按钮

2)转到属性窗口(右下角)

3)选择活动标签

4)查找点击活动

5)选择DGV_CellClick活动



这就是全部!






使用 DataGridView.CurrentRow [ ^ ]属性和将程序体 Dgv_CellClick 导出到另一个体系中(例如 UpdateText )。



 私有  Sub  DataGridView1_CellClick( ByVal  sender  As  System。 Object , ByVal  e  As  System.Windows。 Forms.DataGridViewCellEventArgs)句柄 DataGridView1.CellClick 
UpdateText()
结束 Sub

私有 Sub Button1_Click( ByVal sender As System。 Object ByVal e As System.EventArgs) Handles Button1.Click
UpdateText()
结束 Sub

私有 Sub UpdateText()
Dim cr As DataGridViewRow = Nothing
尝试
cr = .DataGridView1.CurrentRow
' get数据
' 更新文本
.Txt_Name.Text = 新文字

Catch ex As 异常
MsgBox(Err.Description,MsgBoxStyle.Exclamation, 错误
最后
cr = 没什么
结束 尝试

结束 Sub



[\EDIT]


Dear Sir,

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

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

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).

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]