簡體   English   中英

如何獲取每行的datagridview數據並將其顯示在ANOTHER FORM的文本框中? VB.Net

[英]How do I get the data of datagridview per row and show it in the textboxes in the ANOTHER FORM? VB.Net

我有兩種形式。 一個是在DataGridView位於另一個是由一組文本框EditPage.vbMain.vb。 如果我在datagridview中單擊一行,然后在Main.vb中的datagridview中單擊編輯按鈕 ,它將進入EditPage.vb 然后, 單擊行的數據將顯示在EditPage.vb文本框中 ...我將嘗試公開我的變量 ,並將其設置為其他形式(EditPage.vb),但所選行的數據仍然沒有。 t出現。 即使沒有錯誤 ,我的代碼有什么問題?

這是我的datagridview單元格內容的代碼單擊 Main.vb

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

這是Main.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的文本框中 顯示它

為綁定源聲明一個類(窗體級別)級別的公共變量。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM