views:

9766

answers:

7

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. Thanks for any help.

+3  A: 

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
lc
So I have to find each cell that the user selects?
Kind of, but that's not exactly how I'd put it. You don't have to "find" the cells, as a reference to them is presented to you in the collection.
lc
A: 

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:

msdn

ofer
A: 

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();
Cyril Gupta
A: 

Try this:

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

It should work :)

Lullly
A: 

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

code46
A: 

not working for me

rahul
A: 

i am very thankful to u

shaukat ali abbasi