如何获取datagridview的行值,并使用对齐行中的按钮将其传递给另一个表单 [英] How to get row values of a datagridview and pass it to another form using a button in thesame row

查看:95
本文介绍了如何获取datagridview的行值,并使用对齐行中的按钮将其传递给另一个表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Public Sub LoadDataAS()
    grdApplicantsAS.DataSource = Nothing
    grdApplicantsAS.Columns.Clear()
    txtSearchBar1.Clear()
    mycom.Connection = cn
    mycom.CommandText = "SELECT concat_ws(',', LastName, FirstName, MiddleName) as 'Applicant Name',EDate as 'Date of Examination', EScore as 'Examination Score', IStatus as 'Interview Status',IComment as 'Interview Comment' FROM tbl_applicant"

    Dim myadap As New MySqlDataAdapter(mycom)
    Dim mydt As New DataTable

    myadap.Fill(mydt)
    grdApplicantsAS.DataSource = mydt
    Dim buttonColumn As New DataGridViewButtonColumn()
    buttonColumn.HeaderText = ""
    buttonColumn.Name = "Status Request"
    buttonColumn.Text = "Hire"
    buttonColumn.UseColumnTextForButtonValue = True
    grdApplicantsAS.Columns.Add(buttonColumn)
    Dim buttonColumn1 As New DataGridViewButtonColumn()
    buttonColumn1.HeaderText = ""
    buttonColumn1.Name = "Status Request"
    buttonColumn1.Text = "Regrets"
    buttonColumn1.UseColumnTextForButtonValue = True
    grdApplicantsAS.Columns.Add(buttonColumn1)

    myadap.Dispose()
    mydt.Dispose()
End Sub

我的datatable的每一行分别有两个按钮,分别收到和遗憾,我的数据表中的一列包含电话号码,我无法确定,如果我将如何获取我点击按钮的同一行数并将其传递给另一种形式..任何帮助非常感谢

Every row of my datatable has two buttons hire and regrets respectively, a column of my data table contains phone numbers, i cant figure out, if how will i get the number of the same row where i clicked the button hire and pass it to another form.. Any help is greatly appreciated

推荐答案

为此,您可以使用 DataGridView 的CellClick ,请参见下文。

To do this you can use the CellClick event of your DataGridView, please see below.

  Private Sub grdApplicantsAS_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles grdApplicantsAS.CellClick
        Dim frm2 As Form2
        Try
            'First column button
            If e.ColumnIndex = 0 Then
               frm2 = New Form2(grdApplicantsAS.CurrentRow.Cells(YOUR NUMBER COLUMN).Value.ToString())
               frm2.ShowDialog()
            ElseIf e.ColumnIndex = 1
               'Do what you need here for the other button...
            End If             

        Catch ex As Exception
        End Try
    End If
End Sub

FORM 2示例

 Public Class Form2
   Public Property EmployeeName As String

   'Pass your name in as the argument. Now Form 2 will have your name...
   Public Sub New(ByVal strEmployeeName As String)

     'Set your property of your employee name
     EmployeeName = strEmployeeName

   End Sub

 End Class

这篇关于如何获取datagridview的行值,并使用对齐行中的按钮将其传递给另一个表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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