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.
This question is related to
vb.net
datagridview
textbox
tostring
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.
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
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
Try this:
Dim i = Datagridview1.currentrow.index
textbox1.text = datagridview1.item(columnindex, i).value
It should work :)
or, we can use something like this
dim i = dgv1.CurrentCellAddress.X
dim j = dgv1.CurrentCellAddress.Y
MsgBox(dgv1.Item(i,j).Value.ToString())
Simply
MsgBox(GridView1.CurrentCell.Value.ToString)
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();
Source: Stackoverflow.com