Click to See Complete Forum and Search --> : How to get DataGrid Field value for Selected Row?


cgchris99
August 23rd, 2002, 02:09 PM
I have a Datagrid that is from a table.
When I highlight the row, and click my select button, how do I know what the value of a certain column is?

Thanks for any advice

MountnKen
August 28th, 2002, 10:11 AM
When you create the grid using a loop (for I= 1 to 300, next), as you are placing the values in the recordset you will want to create another object with the same value as the column you are creating and then create an array using the same variable ("I")

In other words, you have columns with apples, oranges, and peaches. The rows have values of the pounds sold per day. To save a value of a particular day to use in another form, etc, you need to create a secondary value for the fruit.

lets say we just use the first letter of the fruit for this variable. When you loop through the grid like this:

dim a(1 to 300) as string
dim o(1 to 300) as string
dim p(1 to 300) as string

recordset.fields.append = Apples, adBSTR
recordset.fields.append = Oranges. adBSTR
recordset.fields.append = Peaches, adBSTR

I=1

for I - 1 to 300

recordset. addnew

recordset("Apples").value= databaseapplenum
a(I)= databaseapplenum
recordset("Oranges").value= databaseorangnum
o(I) = databaseorangnum
recordset("Peaches").value= databasepeachnum
P(I) = databasepeachnum


next

~~~~~~~~~~~~~

so when you want to click on a row and get the value of a frame, you call it like this it like this: (the datagrid starts off at row 0, so you have to always add 1)

OrangeSalesOnDay= o(datagrid.row + 1)
AppleSalesOnDay=a(datagrid.row+1)
PeachSalesOnDay= p(datagrid.row+1)


There may be other ways to do this that are "cleaner" than what I am doing, but this works fine for my needs.

Hope this helps!

belthree
September 3rd, 2002, 02:15 PM
You want to use the CurrentCell property of the DataGrid class. It is of type DataGridCell.
This returns the cell that has focus so even if it was not selected(double clicked) it will get returned. So you could do a couple of different things. Add an event handler to your datagrid object for the Double Click event. Or use the IsSelected function of the datagrid class. Whether you need or want to worry about that or not the DataGridCell class has the column and row values of type integer. You can then use the DataGrid.Item function and it will return the value. Example:

dim dGridCell as DataGridCell
dGridCell = dataGrid.CurrentCell
value = dataGrid.Item(dGridCell)
' or you can say
value = dataGrid(dGridCell) ' doesn't matter

I left out a bunch of details but you get the idea.

jhoanofarch
November 27th, 2004, 12:51 AM
How can I get a cell value of a particular column on a datagrid and is not the currentcell? I

I want to add the two columns and the sum will be placed on the current cell.

Can anyone help me on this? thanks in advace!!!

safuanlatif
March 31st, 2010, 09:56 PM
How can I get a cell value of a particular column on a datagrid and is not the currentcell? I

I want to add the two columns and the sum will be placed on the current cell.

Can anyone help me on this? thanks in advace!!!

My answer here is based on winform application for mobile using C# 2005 programming language...

To get the value from any cell, you simply use the name of the datagrid control along with the row and column index of the particular cell that you want to retrieve its value. The return value is always of object type. You may need to cast the type to a specific type of the property or variable that will receive the value.

Syntax: object variable = datagrid[rowindex, columnindex];

Example, let's say the name of the datagrid control is DataGrid1. And you want to retrieve the value of the 4th cell in the selected row and pass it to a string variable named strVar1. Your code will be like this:

string strVar1 = Convert.ToString(DataGrid1[DataGrid1.CurrentRowIndex, 3]);

Remember that the row and column index is zero-based index. Hence, index for 4th column will be 3.

DataMiser
April 1st, 2010, 01:15 AM
6 years? A little late on that one I think :)

safuanlatif
April 1st, 2010, 01:27 AM
6 years? A little late on that one I think :)

I think that's okay. There are many other people also want to get similar information, right?

dglienna
April 1st, 2010, 02:00 AM
winform application for mobile using C# 2005 programming language...

I wouldn't bet on it...