在 vb.net 的文本框中显示 datagridview 单元格数据

Posted

技术标签:

【中文标题】在 vb.net 的文本框中显示 datagridview 单元格数据【英文标题】:display datagridview cell data in the textbox in vb.net 【发布时间】:2015-02-21 07:25:12 【问题描述】:

我想在相关文本框中显示我的 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中的解决方案

【问题讨论】:

您不需要任何代码。将数据放入某种列表中,然后将该列表简单地绑定到网格和各个控件。你想要发生的事情就会自动发生。 【参考方案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

【讨论】:

【参考方案2】:

请使用这个,

Private Sub DataGridView1_CellContentClick_1(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick

    namatxt.Text = Me.DataGridView1.SelectedCells(1).Value.ToString

End Sub

【讨论】:

【参考方案3】:

这个怎么样?

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

【讨论】:

【参考方案4】:

使用此代码,它对我来说很好用:

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”“firstname”“lastname”)等替换为您的列名。

【讨论】:

【参考方案5】:

要使用上下键导航 DataGridView 行并在文本框中显示所选记录,您可以使用以下命令:

'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

另一种方法是使用 datagridview CellEnter 事件

  Private Sub DataGridView1_CellEnter(sender As Object, e As EventArgs) 
  Handles DataGridView1.CellEnter

        Dim row As DataGridViewRow = DataGridView1.CurrentRow
        txtBox1.Text = row.Cells(0).Value.ToString()
        txtBox2.Text = row.Cells(1).Value.ToString(), 

  End Sub

【讨论】:

【参考方案6】:
Private Sub DataGridView1_CellEnter(sender As Object, e As EventArgs) 
  Handles DataGridView1.CellEnter

        Dim row As DataGridViewRow = DataGridView1.CurrentRow
        txtBox1.Text = row.Cells(0).Value.ToString()
        txtBox2.Text = row.Cells(1).Value.ToString(), 

End Sub

【讨论】:

Raju,我怀疑这是否有帮助,甚至根本有效。为了说服我,请解释这是如何工作的以及为什么它应该有所帮助。

以上是关于在 vb.net 的文本框中显示 datagridview 单元格数据的主要内容,如果未能解决你的问题,请参考以下文章

从 vb.net 中的表单外部获取突出显示的文本

vb.net datagridview 单击后指定列的内容放置到相同名称的文本框中

如何将新行添加到数据表 vb.net

VB.NET 中的 Val() 函数是如何工作的?

VB.NET操作SQL SERVER 2008数据库,想把文本框中输入的字符串转化为数字进行数据库的更新,却一直失败

使用 vb.net 根据 SQL 数据库中存在的记录动态显示/隐藏 DataGrid 按钮列时遇到问题