我正在尝试将数据从DataGridview加载到文本框中,但它总是出现错误
我有一个DatagridView,它在表单加载时加载一些数据,它包含从数据库加载的六(6)列数据。一旦我单击任何行,它应该会显示row to TextBoxes available的内容。
我一直收到这样的错误:“索引超出范围必须是非负的,并且小于集合参数索引的大小”
Try
Dim current_row As Integer = DataGridView1.CurrentRow.Index
txt_Prod_ID.Text = DataGridView1(0, current_row).Value
txtprodname.Text = DataGridView1(1, current_row).Value
txt_Barcode.Text = DataGridView1(2, current_row).Value
txt_barcode2.Text = DataGridView1(3, current_row).Value
txt_barcode3.Text = DataGridView1(4, current_row).Value
txt_barcode4.Text = DataGridView1(5, current_row).Value
Catch ex As Exception
MsgBox(ex.Message)
Finally
End Try
我希望当我单击任何行时,它应该会显示文本框中的内容。请协助。
发布于 2019-09-01 07:27:27
这段代码对我来说很有效。我在一些列中有空值,这只会将文本框留空。
Private Sub DataGridView1_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView1.SelectionChanged
Dim current_row As Integer = DataGridView1.CurrentRow.Index
Debug.Print(current_row.ToString)
TextBox1.Text = DataGridView1(0, current_row).Value.ToString
TextBox2.Text = DataGridView1(1, current_row).Value.ToString
TextBox3.Text = DataGridView1(2, current_row).Value.ToString
TextBox4.Text = DataGridView1(3, current_row).Value.ToString
End Sub
我通过调用下面的sub来加载form load中的网格。
Private Sub FillBuildersGrid()
Dim dt As New DataTable
Using cn As New SqlConnection(My.Settings.BuildersConnectio)
Using cmd As New SqlCommand("Select * From Builders", cn)
cn.Open()
dt.Load(cmd.ExecuteReader)
End Using
End Using
DataGridView1.DataSource = dt
End Sub
发布于 2019-09-01 18:52:00
请检查任何单元格的值,无论是dbnull、empty还是"",都可以使用此方法:
If IsDBNull(DataGridView1(0, current_row).Value) Then
TextBox1.Text = ""
Else
TextBox1.Text = DataGridView1(0, current_row).Value.ToString
End If
发布于 2019-09-03 12:31:41
正如其他人所提到的,如果文本框设置正确,使用BindingSource
将“自动”为您完成此操作。使用DataTable
本身也可以做到这一点,但是,下面的示例将DataTable
用作BindingSource
的DataSource
,将BindingSource
用作DataGridView
的DataSource
,并将其用作TextBoxes.
的DataBinding
当“绑定”TextBox
时,每个文本框都需要标识要“绑定”到BindingSource
中的哪个“列”。然后,当用户选择网格中的单元格/行时,文本框将更改以匹配网格中的“选定”行。显然,如果用户可以选择“multiselect”,那么文本框将显示最后选择的行。
注意:当“选定的行”发生变化时,绑定源将“更新”。这意味着如果您更改网格中的一个值,然后选择“相同”行…中的另一个单元格文本框不会更新。一种可能的解决方案是连接网格单元格值更改事件,以简单地“重置”绑定源。无论选择哪个单元格,此操作都将更新文本框。
在此示例中,可以更改文本框中的文本,并且它将在网格中反映/更新,但是,如果文本更改并且用户“离开”单元格,则可能/将有必要重置绑定源。
下面的代码应该正确地将TextBox
绑定到BindingSource
(GridBinding)中的一个列。
Private Sub BindTextBoxes()
TextBox1.DataBindings.Add(New Binding("Text", GridBinding, "ID"))
TextBox2.DataBindings.Add(New Binding("Text", GridBinding, "Name"))
TextBox3.DataBindings.Add(New Binding("Text", GridBinding, "Barcode"))
TextBox4.DataBindings.Add(New Binding("Text", GridBinding, "Barcode2"))
TextBox5.DataBindings.Add(New Binding("Text", GridBinding, "Barcode3"))
End Sub
使用上述方法的一个小示例如下所示。
Dim GridTable As DataTable
Dim GridBinding As BindingSource
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
GridTable = GetTable()
FillTable(GridTable)
GridBinding = New BindingSource()
GridBinding.DataSource = GridTable
DataGridView1.DataSource = GridBinding
BindTextBoxes()
End Sub
Private Function GetTable() As DataTable
Dim dt = New DataTable()
dt.Columns.Add("ID", GetType(String))
dt.Columns.Add("Name", GetType(String))
dt.Columns.Add("Barcode", GetType(String))
dt.Columns.Add("Barcode2", GetType(String))
dt.Columns.Add("Barcode3", GetType(String))
Return dt
End Function
Private Sub FillTable(dt As DataTable)
For index = 1 To 15
dt.Rows.Add("C0R" + index.ToString(), "C1R" + index.ToString(), "C2R" + index.ToString(), "C3R" + index.ToString(), "C4R" + index.ToString())
Next
End Sub
Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
GridBinding.ResetBindings(False)
End Sub
希望这能有所帮助!
https://stackoverflow.com/questions/57740740
复制相似问题