vb.net 从 DataGridView 选定单元格获取文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/724146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:09:34  来源:igfitidea点击:

Get text from DataGridView selected cells

vb.netdatagridviewtextboxtostring

提问by Lullly

I have a DataGridView with cells from a database file that contains data. Basically, I want to get the text from the selectedcells in the DataGridView and display it in a textbox at the click of the button. The code for the button click event is:

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

However in TextBox1I get:

但是在TextBox1 中,我得到:

System.Windows.Forms.DataGridViewSelectedCellCollection

System.Windows.Forms.DataGridViewSelectedCellCollection

I'm thinking it isn't as simple as it seems. I'm a C developer just learning VB.NET.

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

回答by lc.

DataGridView.SelectedCellsis a collection of cells, so it's not as simple as calling ToString()on it. You have to loop through each cell in the collection and get each cell's value instead.

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

The following will create a comma-delimited list of all selected cells' values.

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

C#

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(Translatedfrom the code above)

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

回答by Lullly

Try this:

尝试这个:

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

It should work :)

它应该工作:)

回答by mands

Simply

简单地

MsgBox(GridView1.CurrentCell.Value.ToString)

回答by Wyt

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

回答by alldayremix

A lot of the answers on this page only apply to a single cell, and OP asked for allthe selected cells.

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

If all you want is the cell contents, and you don't care about references to the actual cells that are selected, you can just do this:

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

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

When Button1is clicked, this will fill TextBox1with the comma-separated values of the selected cells.

Button1被点击时,这能满足TextBox1与所选择的小区的逗号分隔的值。

回答by code46

the Best of both worlds.....

两全其美的.....

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

回答by Peter Juanda

or, we can use something like this

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

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

回答by ofer

In this specific case, the ToString() will return the name of the object retruned by the SelectedCell Property.( a collection of the currently selected cells).

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

This behavior occurs when an object has no specific implenetation for the ToString() methods.

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

in our case, all you have to do is to iterate the collection of the cells and to accumulate its values to a string. then push this string to the TextBox.

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

have a look here how to implement the iteration:

看看这里如何实现迭代:

msdn

msdn

回答by Cyril Gupta

Or in case you just need the value of the first seleted sell (or just one selected cell if one is selected)

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

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