This is a migrated thread and some comments may be shown as answers.

[Solved] How to read cell values from selected row from a button click event?

5 Answers 954 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Louis
Top achievements
Rank 1
Louis asked on 08 Feb 2011, 06:51 PM
I've added a button to display on each row of a GridView control.  When the button on the row is selected, I would like to read through the values in the cells for the selected row and pass these as parameters to another method in the program   Also, one of the cells in the row is using a radNumericUpDown control to set a quantity, so I need this value as well. 

I'm currently using VB .NET  as the language for the program.  What are the steps to read the contents of cells of a selected row as well as the values of controls that may be used within a cell?
Thanks 

5 Answers, 1 is accepted

Sort by
0
Louis
Top achievements
Rank 1
answered on 08 Feb 2011, 08:40 PM
I would add to this that the quantity control is not part of the binding to the grid as it is not part of the data being bound to and is simply there for user input.  All the data that is bound is for display purposes only, which is why the examples that I've seen for reading values from a selected row will not work as they assume that you will be populating a business object based on what the grid was bound to.  If I did this I would not be able to see the quantity entered by the user as this is not part of that object.
0
Maya
Telerik team
answered on 09 Feb 2011, 08:33 AM
Hi Louis,

Generally, the DataContext of the button defined in a column would be its corresponding item. So, you may use it for finding the values you want. 
Indeed, the recommended approach is to have a business object with exposed properties used for the source of the grid. Thus the values will be kept even when scrolling. Otherwise, if not properties to bind to are available, you will loose the data as the virtualization is turned on by default. 
How exactly do you populate the RadGridView ?
 

Regards,
Maya
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Louis
Top achievements
Rank 1
answered on 09 Feb 2011, 03:59 PM
Hi Maya,

      Oh the grid is bound to a RIA object for the majority of the columns in the grid.   Only the command button and Numeric UpDown control are not bound to anything.   The idea is to extract data from the selected row to pass to another RIA object to populate a shopping cart.  While waiting on a response, I did discover a way to extract the data I needed to pass this to the other RIA process.  Here's is what I'm doing to get the information from the bound columns:
Dim senderElement As FrameworkElement = TryCast(e.OriginalSource, FrameworkElement)
        Dim row As GridViewRow = senderElement.ParentOfType(Of GridViewRow)()
  
        If row IsNot Nothing Then
                
            If row.Cells.Count > 0 Then
                Dim _ItemKey As String = row.Cells(kColItemKey).Content.Text
                Dim _ItemID As String = row.Cells(kColItemID).Content.Text
                Dim _ItemDesc As String = row.Cells(kColItemDesc).Content.Text
                Dim _ItemPrice As String = row.Cells(kColListPrice).Content.Text
  
                Dim tester As String
  
                tester = String.Format("Item Key; {0}" & vbCrLf &
                                                      "Item ID: {1}" & vbCrLf &
                                                      "Item Desc: {2}" & vbCrLf &
                                                      "Item Price: {3}", _ItemKey, _ItemID, _ItemDesc, _ItemPrice)
                MessageBox.Show(tester, "Row Info", MessageBoxButton.OK)
  
            End If
             
  
  
        End If

The kCol constants are the actual column numbers within the grid.  This seems to work like a champ no matter what row I select on the grid, so part of my problem is solved.   The final piece of the problem is getting the value of the RadNumericUpDown control I have placed in a column to allow a user to select a specific quantity.   Using the technique above, the value in the content is nothing, which makes sense as it isn't actually the column cell holding the information put the control placed within the cell.  So is there a way to read the value of a control set up within a cell template?

Thanks
0
Louis
Top achievements
Rank 1
answered on 09 Feb 2011, 04:23 PM
Hi Maya,

    Ok I figured out the last part of the problem.   Drilling into the row object in debug I discovered if I simply did this:

Dim _QtySelected As String = row.Cells(kColQty).Content.Value

    I was able to retrieve the value of the RadNumericUpDown control..    So unless you see any issues with what I'm doing, I guess I'll go this route for now.   Thanks for the help.
0
Maya
Telerik team
answered on 09 Feb 2011, 07:10 PM
Hello Louis,

I am glad that you found your solution. Generally, as mentioned previously, it is usually recommended to bind the element to particular property from the business object so that you may retrieve them easily, ensure they will not be lost on scrolling and that the correct value is taken. However, as you say that you do not require to bind the element to a property, you may get its value following the approach you choose. 

Greetings,
Maya
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
Tags
GridView
Asked by
Louis
Top achievements
Rank 1
Answers by
Louis
Top achievements
Rank 1
Maya
Telerik team
Share this question
or