7

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

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

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

4

9 回答 9

8

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(从上面的代码翻译)

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
于 2009-04-07T04:37:08.630 回答
7

试试这个:

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

它应该工作:)

于 2010-02-21T11:16:00.380 回答
5

简单地

MsgBox(GridView1.CurrentCell.Value.ToString)
于 2011-05-15T06:35:32.667 回答
2
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
于 2012-08-07T20:11:07.037 回答
1

或者,我们可以使用这样的东西

dim i = dgv1.CurrentCellAddress.X
dim j = dgv1.CurrentCellAddress.Y
MsgBox(dgv1.Item(i,j).Value.ToString())
于 2010-12-23T09:14:04.447 回答
1

此页面上的许多答案仅适用于单个单元格,并且 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所选单元格的逗号分隔值。

于 2013-03-12T20:55:17.690 回答
0

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

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

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

看看这里如何实现迭代:

微软

于 2009-04-07T04:56:04.637 回答
0

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

TextBox1.Text = SelectedCells[0].Value.ToString();
于 2009-04-07T04:59:21.213 回答
0

两全其美的.....

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
于 2010-04-09T15:25:29.010 回答