我想在相关的文本框中显示我的datagridview1数据。当我选择datagridview1中的任何单元格时,相关数据应显示在文本框中。 这是我做的代码
Private Sub DataGridView1_CellClick(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
Handles DataGridView1.CellClick
Dim i As Integer
i = DataGridView1.CurrentRow.Index
Me.Label8.Text = DataGridView1.Item(0, i).Value
Me.TextBox1.Text = DataGridView1.Item(1, i).Value
Me.TextBox2.Text = DataGridView1.Item(2, i).Value
Me.ComboBox1.Text = DataGridView1.Item(3, i).Value
Me.ComboBox2.Text = DataGridView1.Item(4, i).Value
Me.TextBox5.Text = DataGridView1.Item(5, i).Value
Me.TextBox3.Text = DataGridView1.Item(6, i).Value
Me.TextBox4.Text = DataGridView1.Item(7, i).Value
Me.RichTextBox1.Text = DataGridView1.Item(8, i).Value
Me.RichTextBox2.Text = DataGridView1.Item(9, i).Value
Me.Label14.Text = DataGridView1.Item(10, i).Value
End Sub
我在这里做了另一个代码
Private Sub DataGridView1_CellContentClick(ByVal sender _
As System.Object, ByVal e As _
System.Windows.Forms.DataGridViewCellEventArgs) _
Handles DataGridView1.CellContentClick
Try
If e.RowIndex >= 0 Then
Dim row As DataGridViewRow
row = Me.DataGridView1.Rows(e.RowIndex)
Label8.Text = row.Cells("id").Value.ToString
TextBox1.Text = row.Cells("firstname").Value.ToString
TextBox2.Text = row.Cells("lastname").Value.ToString
ComboBox1.Text = row.Cells("year").Value.ToString
ComboBox2.Text = row.Cells("month").Value.ToString
TextBox5.Text = row.Cells("gender").Value.ToString
TextBox3.Text = row.Cells("address").Value.ToString
TextBox4.Text = row.Cells("telephone").Value.ToString
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
然而,两个代码都不起作用,它们会出错。在""内标记代表列名称
任何人都可以在vb.net中给我解决方案
答案 0 :(得分:1)
Private Sub DataGridView1_CellClick (ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim i As Integer
With DataGridView1
if e.RowIndex >= 0 Then
i = .CurrentRow.Index
tbID.text = .Rows(i).Cells("id").Value.ToString
tbFirstNametext = .Rows(i).Cells("firstname").Value.ToString
tbLastNametext = .Rows(i).Cells("lastname").Value.ToString
tbAddresstext = .Rows(i).Cells("address").Value.ToString
End If
End With
End Sub
答案 1 :(得分:0)
这个怎么样?
Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim row As DataGridViewRow = DataGridView1.CurrentRow
Me.Label8.Text = row.Cells(0).Value.ToString()
Me.TextBox1.Text = row.Cells(1).Value.ToString()
Me.TextBox2.Text = row.Cells(2).Value.ToString()
Me.ComboBox1.Text = row.Cells(3).Value.ToString()
Me.ComboBox2.Text = row.Cells(4).Value.ToString()
Me.TextBox5.Text = row.Cells(5).Value.ToString()
Me.TextBox3.Text = row.Cells(6).Value.ToString()
Me.TextBox4.Text = row.Cells(7).Value.ToString()
Me.RichTextBox1.Text = row.Cells(8).Value.ToString()
Me.RichTextBox2.Text = row.Cells(9).Value.ToString()
Me.Label14.Text = row.Cells(10).Value.ToString()
End Sub
答案 2 :(得分:0)
使用此代码,它适用于我:
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
If e.RowIndex >= 0 Then
Dim row As DataGridViewRow
row = Me.DataGridView1.Rows(e.RowIndex)
tbID.Text = row.Cells("id").Value.ToString
tbFirstName.Text = row.Cells("firstname").Value.ToString
tbLastName.Text = row.Cells("lastname").Value.ToString
tbAddress.Text = row.Cells("address").Value.ToString
End If
End Sub
使用您的列名替换我所拥有的列的名称(即" id""名字""姓氏")等。
答案 3 :(得分:0)
请使用此,
Private Sub DataGridView1_CellContentClick_1(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
namatxt.Text = Me.DataGridView1.SelectedCells(1).Value.ToString
End Sub
答案 4 :(得分:0)
要使用向上和向下键导航DataGridView行并在文本框中显示所选记录,您可以使用:
$complete = "Success!"
另一种方法是使用datagridview CellEnter事件
'declare variable
Private DB As New databasenameDataSetTableAdapters.tablenameTableAdapter
'add constructor to your form
Public Sub New()
' This call is required by the designer.
InitializeComponent()
'Setting KeyPreview To True makes sure that the Form1_KeyDown will always
'be called If a key Is pressed,
Me.KeyPreview = True
End Sub
Private Sub form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
'if else statement is there otherwise when going through the rows in grid
'it would show the previous row in textboxes instead of current one
'GetData is a select query you can generate by double clicking the dataset.xsd file in solution explorer
Case Keys.Up
Dim rowUp As Integer = dataGridView.CurrentRow.Index
If (rowUp <= 0) Then
rowUp = 0
Else
rowUp = rowUp - 1
End If
Try
txtBox1.Text = DB.GetData.Rows(rowUp).Item(0).ToString
txtBox2.Text = DB.GetData.Rows(rowUp).Item(1).ToString
txtBox3.Text = DB.GetData.Rows(rowUp).Item(2).ToString
Catch ex As Exception
MsgBox(ex.Message)
End Try
Exit Select
Case Keys.Down
Dim rowDown As Integer = dataGridView.CurrentRow.Index
If (rowDown >= DB.GetData.Rows.Count - 1) Then
rowDown = DB.GetData.Rows.Count - 1
Else
rowDown = rowDown + 1
End If
Try
txtBox1.Text = DB.GetData.Rows(rowDown).Item(0).ToString
txtBox2.Text = DB.GetData.Rows(rowDown).Item(1).ToString
txtBox3.Text = DB.GetData.Rows(rowDown).Item(2).ToString
Catch ex As Exception
MsgBox(ex.Message)
End Try
Exit Select
答案 5 :(得分:0)
sonata_admin.yaml