【问题标题】:Get text from DataGridView selected cells从 DataGridView 选定的单元格中获取文本
【发布时间】:2010-10-17 23:19:12
【问题描述】:

我有一个 DataGridView,其中包含来自包含数据的数据库文件的单元格。基本上,我想从 DataGridView 中的 selected 单元格中获取文本,并在单击按钮时将其显示在文本框中。按钮点击事件的代码为:

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim SelectedThings As String = DataGridView1.SelectedCells.ToString
    TextBox1.Text = SelectedThings
End Sub

但是在 TextBox1 我得到:

System.Windows.Forms.DataGridViewSelectedCellCollection

我认为这并不像看起来那么简单。我是一名 C 开发人员,刚刚学习 VB.NET。

【问题讨论】:

    标签: vb.net datagridview textbox tostring


    【解决方案1】:

    DataGridView.SelectedCells 是一个单元格的集合,所以并不像在其上调用ToString() 那样简单。您必须遍历集合中的每个单元格并获取每个单元格的值。

    以下将创建所有选定单元格值的逗号分隔列表。

    C#

    TextBox1.Text = "";
    bool FirstValue = true;
    foreach(DataGridViewCell cell in DataGridView1.SelectedCells)
    {
        if(!FirstValue)
        {
            TextBox1.Text += ", ";
        }
        TextBox1.Text += cell.Value.ToString();
        FirstValue = false;
    }
    

    VB.NET(来自上述代码的Translated

    TextBox1.Text = ""
    Dim FirstValue As Boolean =  True 
    Dim cell As DataGridViewCell
    For Each cell In DataGridView1.SelectedCells
        If Not FirstValue Then
            TextBox1.Text += ", "
        End If
        TextBox1.Text += cell.Value.ToString()
        FirstValue = False
    Next
    

    【讨论】:

    • 所以我必须找到用户选择的每个单元格?
    • 有点意思,但我不是这么说的。您不必“找到”单元格,因为在集合中会向您提供对它们的引用。
    • VB.NET 版本的小改进:用 &= 代替 +=(当然都可以)。
    【解决方案2】:

    在这种特定情况下,ToString() 将返回由 SelectedCell 属性返回的对象的名称。(当前选定单元格的集合)。

    当对象没有特定的 ToString() 方法实现时,会发生此行为。

    在我们的例子中,您所要做的就是迭代单元格的集合并将其值累积到一个字符串中。然后将此字符串推送到文本框。

    看看这里如何实现迭代:

    msdn

    【讨论】:

      【解决方案3】:

      或者,如果您只需要第一个选定的卖出值(或者如果选择了一个,则只需要一个选定的单元格)

      TextBox1.Text = SelectedCells[0].Value.ToString();
      

      【讨论】:

        【解决方案4】:

        试试这个:

        Dim i = Datagridview1.currentrow.index
        textbox1.text = datagridview1.item(columnindex, i).value
        

        它应该工作:)

        【讨论】:

          【解决方案5】:

          两全其美.....

          Private Sub tsbSendNewsLetter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbSendNewsLetter.Click
                  Dim tmpstr As String = ""
                  Dim cnt As Integer = 0
                  Dim virgin As Boolean = True
                  For cnt = 0 To (dgvDetails.Rows.Count - 1)
                      If Not dgvContacts.Rows(cnt).Cells(9).Value.ToString() Is Nothing Then
                          If Not dgvContacts.Rows(cnt).Cells(9).Value.ToString().Length = 0 Then
                              If Not virgin Then
                                  tmpstr += ", "
                              End If
                              tmpstr += dgvContacts.Rows(cnt).Cells(9).Value.ToString()
                              virgin = False
                              'MsgBox(tmpstr)
                          End If
                      End If
                  Next
                  Dim email As New qkuantusMailer()
                  email.txtMailTo.Text = tmpstr
                  email.Show()
              End Sub
          

          【讨论】:

            【解决方案6】:

            或者,我们可以使用类似的东西

            dim i = dgv1.CurrentCellAddress.X
            dim j = dgv1.CurrentCellAddress.Y
            MsgBox(dgv1.Item(i,j).Value.ToString())
            

            【讨论】:

              【解决方案7】:

              简单

              MsgBox(GridView1.CurrentCell.Value.ToString)
              

              【讨论】:

                【解决方案8】:
                Private Sub DataGridView1_CellClick(ByVal sender As System.Object, _
                                                    ByVal e As DataGridViewCellEventArgs) _
                                                    Handles DataGridView1.CellClick
                    MsgBox(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value)
                End Sub
                

                【讨论】:

                  【解决方案9】:

                  此页面上的许多答案仅适用于单个单元格,并且 OP 要求所有选定的单元格。

                  如果您想要的只是单元格内容,并且您不关心对所选实际单元格的引用,您可以这样做:

                  Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
                      Dim SelectedThings As String = DataGridView1.GetClipboardContent().GetText().Replace(ChrW(9), ",")
                      TextBox1.Text = SelectedThings
                  End Sub
                  

                  当点击Button1 时,这将用所选单元格的逗号分隔值填充TextBox1

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 2015-12-05
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2013-06-14
                    相关资源
                    最近更新 更多