【问题标题】:How do I get the data of datagridview per row and show it in the textboxes in the ANOTHER FORM? VB.Net如何获取每行 datagridview 的数据并将其显示在 ANOTHER FORM 的文本框中? VB.Net
【发布时间】:2018-05-10 13:01:19
【问题描述】:

我有两种形式。一个是datagridview所在的Main.vb,另一个是EditPage.vb一组文本框。如果我单击 datagridview 中的一行并单击 datagridview编辑按钮 /em> 在 Main.vb 中,它将进入 EditPage.vb。然后,点击的行的数据会显示在EditPage.vb中的文本框...我会尝试public我的变量 并将其设置在另一个 form(EditPage.vb) 但仍然没有显示所选行的数据。我的代码有什么问题即使没有错误?

这是我在 Main.vb

中的 datagridview 单元格内容点击的代码
Private Sub tblAttendance_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles tblAttendance.CellContentClick
    Dim row As DataGridViewRow = tblAttendance.CurrentRow





    Try
        id = row.Cells(0).Value.ToString()
        firstname = row.Cells(1).Value.ToString
        lastname = row.Cells(2).Value.ToString
        birthdate = row.Cells(3).Value.ToString
        position = row.Cells(4).Value.ToString
        sex = row.Cells(5).Value.ToString
        address = row.Cells(6).Value.ToString
        contact_num = row.Cells(7).Value.ToString
        email = row.Cells(8).Value.ToString

    Catch ex As Exception
        MessageBox.Show("Input Data Properly!", "Error Message")
    End Try
End Sub

这是 Edit ButtonMain.vb

中的代码
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Dim result As Integer = MessageBox.Show("Are you sure you want to Edit the Info?", "Validation", MessageBoxButtons.YesNoCancel)

    If DialogResult.Yes Then

        Edit_Page.Show()

    ElseIf DialogResult.No Then
        MessageBox.Show("Edit Cancelled", "Message")

    End If

End Sub

这是我在 Main.vb

中的公共变量
Public id As Integer
Public firstname As String
Public lastname As String
Public birthdate As String
Public position As String
Public sex As String
Public address As String
Public contact_num As String
Public email As String

我的公共变量EditPage.vb中获取公共变量Main.vb

Public id_edit As Integer
Public firstname_edit As String
Public lastname_edit As String
Public birthdate_edit As String
Public position_edit As String
Public sex_edit As String
Public address_edit As String
Public contact_num_edit As String
Public email_edit As String

Edit.Page.vb形式的代码或称为Edit_Page_Load

的代码
 id_edit = Main.id
    firstname_edit = Main.firstname
    lastname_edit = Main.lastname
    birthdate_edit = Main.birthdate
    position_edit = Main.position
    sex_edit = Main.sex
    address_edit = Main.address
    contact_num_edit = Main.contact_num
    email_edit = Main.email


    firstname_txtbox.Text = firstname_edit
    lastname_txtbox.Text = lastname_edit
    DateTimePicker1.Text = birthdate_edit
    position_txtbox.Text = position_edit
    sex_combo.Text = sex_edit
    address_txtbox.Text = address_edit
    contact_mask.Text = contact_num_edit
    email_txtbox.Text = email_edit

再次,我的问题是我无法在 MAIN.VB 中获取我的 DATAGRIDVIEW 的选定行的数据>在 EDITPAGE.VB 的文本框中显示它

【问题讨论】:

  • 不要在你的问题上附加不相关的标签。
  • @TJWolschon 很抱歉,同时编辑。
  • @JacobH 很好,只是希望标签正确。
  • 我可能会为此被钉死在十字架上,但如果您打算以这种方式在这些表单之间共享数据,也许在这里为共享变量设置一个单独的模块比较好?
  • 对不起,但我想找到我罕见问题的答案,因为我在 vb.net 中从未遇到过此类问题。我找不到确切的答案。

标签: vb.net datagridview


【解决方案1】:

为绑定源声明一个类(表单级别)级别的公共变量。

Public bind As New BindingSource()

绑定到您的 DataGridView

Private Sub FillGrid()
        Dim dt As New DataTable
        Using cn As New SqlConnection(My.Settings.CoffeeConnectionString)
            Dim strSQL As String = "Select * From Coffees;"
            Using cmd As New SqlCommand(strSQL, cn)
                'dr As SqlDataReader
                cn.Open()
                Using dr As SqlDataReader = cmd.ExecuteReader
                    dt.Load(dr)
                End Using
            End Using
        End Using
        bind.DataSource = dt
        DataGridView1.DataSource = bind
End Sub

然后在第二个表单上使用第一个表单中的 BindingSource。

Private Sub TestBindingSource_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        txtCoffeeName.DataBindings.Add("Text", ExcelToDGV.bind, "Name")
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-20
    • 1970-01-01
    • 2016-09-14
    • 1970-01-01
    • 2021-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多