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.
Best Answer
DataGridView.SelectedCells
is a collection of cells, so it's not as simple as callingToString()
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#
VB.NET (Translated from the code above)