CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2002
    Posts
    41

    How to get DataGrid Field value for Selected Row?

    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

  2. #2
    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!
    ~~~~~~~~~~~~~~~~~~~~~~~~

    No electrons were harmed in the making of this message.

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

  3. #3
    Join Date
    Aug 2002
    Posts
    2
    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.

  4. #4
    Join Date
    Aug 2004
    Posts
    47

    Cool Re: How to get DataGrid Field value for Selected Row?

    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!!!

  5. #5
    Join Date
    Mar 2010
    Location
    Kuala Lumpur, Malaysia
    Posts
    2

    Re: How to get DataGrid Field value for Selected Row?

    Quote Originally Posted by jhoanofarch View Post
    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.

  6. #6
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: How to get DataGrid Field value for Selected Row?

    6 years? A little late on that one I think

  7. #7
    Join Date
    Mar 2010
    Location
    Kuala Lumpur, Malaysia
    Posts
    2

    Smile Re: How to get DataGrid Field value for Selected Row?

    Quote Originally Posted by DataMiser View Post
    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?

  8. #8
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: How to get DataGrid Field value for Selected Row?

    winform application for mobile using C# 2005 programming language...
    I wouldn't bet on it...
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured