datagrid view, find selected cell

smokin

Member
Joined
Apr 22, 2006
Messages
12
Programming Experience
Beginner
Hi,

I am using Visual Basic 2005. I have a DataGridView control on a windows form.

Now I have populated the DataGridView control via a query from a MySQL Database using the MySQL Connector/Net 1.0.

I am able to populate the DataGridView with a bunch of data. Currently I have the datagrid set as uneditable.

What I want to do is be able to store the data of the SELECTED row when a user clicks once on a row in the DataGridView.

The datagrid view has a TournamentName, TournamentDate and TournamentLocation.

What I am ending up wanting to do, is when the app is run, you highlight a row by clicking once on the datagridview, and then clicking a edit button, after clicking the edit button it is then going to populate another new window form with the details of that tournament.

I had tried doing this,

Dim SelectedRow As ArrayList
SelectedRow = dgvTournaments.SelectedRows

But that just spat a error at build time of how the dgvTournaments.SelectedRows cannot be converted to arraylist.

Pretty much once its in a array then i can just select the TournamentName from it and set it as a variable to pass on to the new edit form.

Any help would be greatly appreciated.

Anyone who knows where to look on the net for a good tutorial of using the datagrid control with the mysql .net connector dll file would also be of great help to me.

thanks.
 
The doc says the dgv.SelectedRows property is of type DataGridViewSelectedRowCollection, and that is the type you should use for your variable if you want to store these.

Alternative is to check if dgv.SelectedRows.Count is more than 0 items, if so just take the first Item like this: dgv.SelectedRows(0)
You can do this instead of dgv.SelectedRows.Item(0) because the Item is default property.

The Items in the SelectedRows collection is of type DataGridViewRow.
 
still confused. I have a datagrid populated by the mysql connector/net.

i have rows and a user can click a row which highlights the entire line. i want to be able to get the variable of the first column's value of the selected row.

i am trying to just code to save that value as a variable and to also display it into label using its .text option.

i tried altering part of the code to this,

Dim SelectedRow As DataGridViewSelectedRowCollection
SelectedRow = dgvTournaments.SelectedRows

lbl2.Text = dgvTournaments.SelectedRows.Item(0)

So ive changed the SelectedRow variable from a arraylist to a DataGridViewSelectedrowCollection. Then the second line i assume says hey lets grab the selected row items and store into that variable. I dont know what the next line would be to get the first column variable of that row though?

The lbl2.text = .... line, I get a error that says 'Value of type 'System.Windows.Forms.DataGridViewRow' cannot be converted to 'String'

Any further help guys? Much appreciated just to get me on my feet here.
 
VB.NET:
lbl2.Text = dgvTournaments.SelectedRows(0).Cells(0).ToString()
 
thanks for the fast reply john. i updated the lbl2.text to what you stated.

i have a form and it has a update butotn at the moment that im after i select a row and then i click that button just as a test with that lbl2.text line to see if it updates that label to the value of the cell on the selected row.

however it shows osmething else, ive attached a screenshot.

any further ideas?

vbnet1.jpg
 
Yes, that was fast. Did you try the Value member of a DataGridViewCell?
VB.NET:
lbl2.Text = dgvTournaments.SelectedRows(0).Cells(0).Value
 
Back
Top