简体   繁体   中英

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

I have two forms. One is the Main.vb which the datagridview is located and the other one is the EditPage.vb which consist a set of textboxes . If I click a row in datagridview and click the edit button in the datagridview in Main.vb it will go in EditPage.vb . Then, the data of the clicked row will show in the textboxes in EditPage.vb ... I'll try to public my variables and set it in the other form(EditPage.vb) but still the data of the selected row didn't shown up. What's something wrong in my code even it has no error?

Here's the code of my datagridview Cell Content Click in 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

Here's the code of my Edit Button in 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

And Here's my Public Variables in 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

My Public Variables in EditPage.vb to get the public variables in 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

The code in the of the form of Edit.Page.vb or called 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

AGAIN, MY PROBLEM IS I CAN'T GET THE DATA OF THE SELECTED ROW OF MY DATAGRIDVIEW IN MAIN.VB AND SHOW IT IN EDITPAGE.VB'S TEXTBOXES .

Declare a class (Form level) level Public variable for a binding source.

Public bind As New BindingSource()

Bind to your 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

Then on the second form use the BindingSource from the first form.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM