从选定的DataGridView单元格获取文本

8

我有一个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

我认为它并不像看起来那么简单。我是一名C开发者,正在学习VB.NET。

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

那么我需要找到用户选择的每个单元格吗? - James
有点像,但不完全是这样。你不需要“查找”单元格,因为集合中已经向你呈现了对它们的引用。 - lc.
1
VB.NET版本的小改进:使用&=代替+=(当然两者都可以)。 - Mark Hurd

7

试试这个:

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

它应该可以工作 :)

5

简单地说

MsgBox(GridView1.CurrentCell.Value.ToString)

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

1

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

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

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

1

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

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

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

0
在这种特定情况下,ToString()将返回SelectedCell属性返回的对象的名称(当前选定单元格的集合)。
当一个对象没有特定的ToString()方法实现时,就会出现这种行为。
在我们的情况下,您只需要迭代单元格的集合并将其值累加到一个字符串中。然后将此字符串推送到TextBox中。
请看这里如何实现迭代: msdn

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接