obtain cell value when user clicks anywhere on the row

vbdot

New member
Joined
Apr 25, 2007
Messages
1
Programming Experience
3-5
I am looking at a way to get the value on column 2, when the user selects a row.
For example: the datagrid shows like this:

Row Name Location
1 Picture 1 c:\pic_1.bmp
2 Picture 2 c:\pic_2.bmp
3 Picture 3 c:\pic_3.bmp

I am able to get the value from using datagrid.currentcell.value.tostring, but I have to click exactly at the cell to get the current value.


If the user click on row 2, I want to get the value from LOCATION columns in row 2 (the value is "c:\pic_2.bmp"), usually in VB 6.0 I would write it like:

dim x as string
x = datagrid.columns(2).value

So x = c:\pic_2.bmp

Right now with datagrid.currentcell.value.tostring, the user have to click on "c:\pic_2.bmp" in order to get the correct value. So is there a way to code it so when user click the row, go get the value from LOCATION column?
 
datagrid, or datagridview? If you have .net 2.0 you ought to be using DataGridView..

Regardless of the grid, it should be bound through a BindingSource, which has a CurrentChanged event (occurs every time the current row the bindingsource is pointing to, changes) hence you do:


DirectCast(xxxBindingSource.Current, DataRowView).Item(1) ' column 2
DirectCast(DirectCast(xxxBindingSource.Current, DataRowView), XXXDataRow).Column2Name ' better

Try to avoid making absolute references to columns by number. Using the name is much better
 
Back
Top