Retrieve value from datagrid column

jdavis2

Member
Joined
Nov 2, 2006
Messages
12
Programming Experience
Beginner
Hello all,

How in the world do I retrieve a value from a specific column within the selected row of a datagrid.

So i have a datagrid that display roughly 10 columns of data. Selection mode is set to FullRow "I would really prefer to keep it that way" So how do select a row hit a button and the populate a textbox with whatever value is in say column 4 of that selected row?

Seems such an easy task yet so hard to figure out.... :confused::confused::eek:
 
something like:

DataGridView1.SelectedRows(0).Cells(3)

Should be fine for a single-selection-mode datagrid. Remember that indexes are 0 based so (0) is the 1st row, (3) is the 4th item

Note also tha tthis code makes use of default properties. It is identical to the longer form of:
DataGridView1.SelectedRows.Item(0).Cells.Item(3)


Broken down it is:
DataGridView1 <-- the datagridview
.SelectedRows <-- a collection of all the selected rows
.Item(0) <-- the first item in that collection, actually returns a DataGridViewRow object
.Cells <-- returns the cells within the datagridviewrow as a cell collection
.Item(3) <--returns the fourth cell in the collection


I'll leave it as an excercise for you to make it more robust in the event that there are 0 rows selected - this code will crash
 
Thanks for the reply again cjard. I initally trie dwhat you had suggested but it complained. So this is what I came up with :

VB.NET:
Dim t AsInteger
t = CInt(TbljobsDataGridView.SelectedRows.Item(0).Cells.Item(0).Value)
TextBox1.Text = t.ToString 'only purpose is to test output

Now let me see if I understand this correctly
VB.NET:
t = CInt(TbljobsDataGridView.SelectedRows.Item(0).Cells.Item(0).Value)

.SelectedRows.Item(0) - SelectedRows means the user could possibly have more than one row selected, so within the selected rows I want row (0). I suppose if only one row was selected and I put (1) it would give me a error.

Cells.Item(0).Value - This is which column of data I would like to select. Now what I am confused on is the .value part. Is it simply just there to specify that I want the contents "value" of the cell I am calling? That would be a logical answer.

Anyway it does work the way I posted the code I just want to make sure I am understanding why it works. The textbox is just to verify it was working, I will need the result as a integer well probably a int16/short "where that note on data types... haha"

Thanks cjard!

P.S - I know Im not using naming conventions ect.. I am usually very strict on final products just very lazy during testing/head scratching/trial and error
 
Last edited by a moderator:
The two codes I posted were syntactically correct and equivalent. The error you encountered was likely because you were attempting to assign the result of my code, to an integer

DataGridView1.SelectedRows(0).Cells(3)


Returns an object of type DataGridViewCell, it does not return an integer. It hence cannot be assigned to an integer.

I apologise for slightly misreading your post - you did indeed say you wanted the vlaue from the cell, whereas the code i gave you gives the cell itself. A DataGridViewCell has many properties. The value is just one of them. To see the others, press F2 and search for DataGridViewCell - whoa!

This gives the value:

DataGridView1.SelectedRows(0).Cells(3).Value

You need to put the value in there so VB knows that you want it.. If you didnt put it there then VB would wonder which of the 50 properties you can access you actually wanted!

As you have found. This value is of type Object which again, cannot be assigned to an integer variable. This is why it must be converted.
You chose to use CInt to convert it; CInt is a wrapper for Convert.ToInt32 and CInt is VB.NET specific. I usually advocate that people should use the proper .NET functions rather than the VB specific ones because they thus get tied into a legacy thought pattern intended to allow VB6 code upgrading. Hence if you'd asked for code to retrieve the numerical value from a cell and assign it to an integer variable I would have said:

Dim i as Integer = Convert.ToInt32(DataGridView1.SelectedRows(0).Cells(3)
.Value)

:D
 
Back
Top