我有两种形式。一个是 datagridview 所在的 Main.vb ,另一个是 EditPage.vb ,其中包含一组 文本框 。如果我点击 datagridview 中的一行,然后点击 datagridview <中的 编辑按钮 Main.vb 中的/ em> 将进入 EditPage.vb 。然后, 点击行 的数据将显示在EditPage.vb 中的文本框中...我将尝试公开我的变量并将其设置为其他表单(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
以下是 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 所选行的数据显示 IN EDITPAGE.VB&#39; TEXTBOXES 。
答案 0 :(得分:0)
声明类(表单级别)级别绑定源的公共变量。
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