如何在按钮单击中调用Datagridview单元格单击事件 [英] How to call Datagridview Cell Click Event in Button Click

查看:113
本文介绍了如何在按钮单击中调用Datagridview单元格单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的先生,



我在datagridview单元格点击活动中使用的以下代码需要帮助。



 私有  Sub  Dgv_CellClick( ByVal  sender 作为 对象 ByVal  e  As  System.Windows.Forms.DataGridViewCellEventArgs) Handles  Dgv.CellClick 
如果 e.RowIndex> = 0 然后
尝试
Dim obj 作为 ConStr
Dim sql 作为 字符串
Dim tbl 作为 New DataTable
Txt_Id.Text = Dgv.Rows(e.RowIndex).Cells( 0 )。价值
sql = 选择*来自AA,其中Code ='& Txt_Id.Text& '
tbl = obj.GetRecords(sql)
Txt_Name.Text = tbl.Rows( 0 )( Nm
Catch ex As IndexOutOfRangeException
MsgBox(ex.Message, MsgBoxStyle.Critical)
结束 尝试
结束 如果
结束 Sub





我的表格中有一个按钮,我想当我点击按钮时,它会调用

在Dgv_CellClick事件之上我提到的代码。



我将为这个过程做些什么。



Rgds

Indranil

解决方案

要做的步骤:

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]


这篇关于如何在按钮单击中调用Datagridview单元格单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆