Get text from DataGridView selected cells
-
05-09-2019 - |
Question
I have a DataGridView with cells from a database file that contains data. Basically, I want to get the text from the selected cells in the DataGridView and display it in a textbox at the click of the button. The code for the button click event is:
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 TextBox1 I get:
System.Windows.Forms.DataGridViewSelectedCellCollection
I'm thinking it isn't as simple as it seems. I'm a C developer just learning VB.NET.
Solution
DataGridView.SelectedCells
is 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.
The following will create a comma-delimited list of all selected cells' values.
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 from the code above)
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
OTHER TIPS
Try this:
Dim i = Datagridview1.currentrow.index
textbox1.text = datagridview1.item(columnindex, i).value
It should work :)
Simply
MsgBox(GridView1.CurrentCell.Value.ToString)
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
A lot of the answers on this page only apply to a single cell, and OP asked for all the selected cells.
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 Button1
is clicked, this will fill TextBox1
with the comma-separated values of the selected cells.
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).
This behavior occurs when an object has no specific implenetation for the ToString() methods.
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:
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();
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
or, we can use something like this
dim i = dgv1.CurrentCellAddress.X
dim j = dgv1.CurrentCellAddress.Y
MsgBox(dgv1.Item(i,j).Value.ToString())