我想在按钮单击上编写一些我动态添加的代码 [英] I want to write some code on button click which i have added dynamically

查看:89
本文介绍了我想在按钮单击上编写一些我动态添加的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

晚上好,
我在DataGridView中有动态添加按钮(C#Windows应用程序).现在,我想在单击该按钮时编写一些代码,有人可以告诉我该怎么做吗?紧急
谢谢

Good Evening,
I have add button in DataGridView dynamically(c# Windows Application). Now I want to write some code on click of that button can anyone tell me how can I do that? Its urgent
Thanks

推荐答案

你好

您可以将btn添加到datagrid view的尝试代码中,如下所示,您肯定会成功


Hello

you can add btn into datagrid view try code as below you will definitely got success


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        DataGridView1.ColumnCount = 3
        DataGridView1.Columns(0).Name = "ID"
        DataGridView1.Columns(1).Name = "Name"
        DataGridView1.Columns(2).Name = "Price"

        Dim row As String() = New String() {"1", "P1", "1000"}
        DataGridView1.Rows.Add(row)
        row = New String() {"2", "P2", "2000"}
        DataGridView1.Rows.Add(row)
        row = New String() {"3", "P3", "3000"}
        DataGridView1.Rows.Add(row)
        row = New String() {"4", "P4", "4000"}
        DataGridView1.Rows.Add(row)

        Dim btn As New DataGridViewButtonColumn()
        DataGridView1.Columns.Add(btn)
        btn.HeaderText = "Click Data"
        btn.Text = "Click Here"
        btn.Name = "btn"
        btn.UseColumnTextForButtonValue = True

    End Sub
    Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        If e.ColumnIndex = 3 Then
            MsgBox(("Row : " + e.RowIndex.ToString & "  Col : ") + e.ColumnIndex.ToString)
        End If
    End Sub


您需要使用DataGridView .CellContentClick事件,用于检测DataGridViewButtonCell的按钮单击,

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellcontentclick.aspx [ ^ ]
You need to use DataGridView.CellContentClick Event to detect button clicks for a DataGridViewButtonCell,

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellcontentclick.aspx[^]


这篇关于我想在按钮单击上编写一些我动态添加的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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