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

How to access contents of selected items?

1 Answer 122 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jeff
Top achievements
Rank 1
Jeff asked on 08 Jan 2009, 04:55 PM
We have a pretty simple grid, with a number of rows and a number of columns.  In each cell we display a string.  The first column contains checkboxes, by which the user can select one or more rows.

When the user clicks on a button, we want to do something - server side - with the data in the selected rows.

In the button click code-behind, I have a reference to the grid.  With that reference, I can access SelectedItems.  But for the life of me I can't figure out how to get the text values that are in the cells.

GridItemCollection selectedItems = grid.SelectedItems; 
foreach (GridItem selectedItem in selectedItems) 
   // Then what? 

All of the examples I've been able to find, that pull data from the items, are passed the items in grid event functions.  When the user clicks on our button, it doesn't generate an event on the grid, so we're not being passed the items, we need to get them from the grid object.  The problem is that the grid events pass GridDataItems, and the grid control contains GridItems, so the access methods described for the one don't work for the other.

How can I access the selected GridDataItems from the grid control, other than in a grid event function?

OK, never mind...

RadGrid.SelectedItems contains GridItems.  All I need do is cast them to GridDataItems, and I can access the text values of the items within it.


1 Answer, 1 is accepted

Sort by
0
Accepted
Daniel
Telerik team
answered on 08 Jan 2009, 05:15 PM
Hello Jeff,

Please examine the following code:
foreach (GridItem selectedItem in selectedItems) 
    string text = ((GridDataItem)selectedItem)["yourColumnName"].Text; 
}  

Another approach:
Hashtable values; 
foreach (GridItem selectedItem in selectedItems) 
    values = new Hashtable(); 
    ((GridDataItem)selectedItem).ExtractValues(values); 
    string myValue = values["name"].ToString(); 

Best regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Jeff
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Share this question
or