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

[Solved] Getting at the selected item data in a GridView

4 Answers 162 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.
scott
Top achievements
Rank 1
scott asked on 28 Jul 2010, 02:20 PM
I have a RadGridView with an ItemsSource set to a DataTable from Vlad's data table class.  Thanks Vlad :)  Anyway, elsewhere I have a button.  Clicking the button means I have to get a field from the selected grid view record and do something with it, but I don't know how to reference the data fields.  The selected item appears to be of type DynamicObject.  I can see the fields, but I don't know how to get them.  For example, I want the string "11" from ObjID in the selected row.

string objid = // what ?

This is a view from my locals window in the debugger:

+  selectedRow {DynamicObjectBuilder_c0987842-f929-4dad-aaf9-329941d0cae5} object {DynamicObjectBuilder_c0987842-f929-4dad-aaf9-329941d0cae5}
-  obj {DynamicObjectBuilder_c0987842-f929-4dad-aaf9-329941d0cae5} Telerik.Data.DynamicObject {DynamicObjectBuilder_c0987842-f929-4dad-aaf9-329941d0cae5}
-  [DynamicObjectBuilder_c0987842-f929-4dad-aaf9-329941d0cae5] {DynamicObjectBuilder_c0987842-f929-4dad-aaf9-329941d0cae5} DynamicObjectBuilder_c0987842-f929-4dad-aaf9-329941d0cae5
+  base {DynamicObjectBuilder_c0987842-f929-4dad-aaf9-329941d0cae5} Telerik.Data.DynamicObject {DynamicObjectBuilder_c0987842-f929-4dad-aaf9-329941d0cae5}
  CreateDate "2010-07-08T05:24:10.54-04:00" string
  ModifyDate "2010-07-19T10:20:34.503-04:00" string
  ObjID "11" string

4 Answers, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 28 Jul 2010, 02:39 PM
Hi scott,

you can cast SelectedItem to DynamicObject and them use the GetValue method to obtain the desired property value.

var selectedItem = this.RadGridView1.SelectedItem as DynamicObject;
var propertyValue = selectedItem.GetValue<int>("ID");


All the best,
Milan
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
scott
Top achievements
Rank 1
answered on 28 Jul 2010, 03:20 PM
Hi Milan - that doesn't compile

Error 1 'Telerik.Data.DynamicObject.GetValue<T>(string)' is inaccessible due to its protection level 

Maybe there's a change I can make in Vlad's DynamicObject class ?

namespace Telerik.Data
{
    public abstract class DynamicObject : INotifyPropertyChanged
    {
        private readonly Dictionary<string, object> valuesStorage;
        public event PropertyChangedEventHandler PropertyChanged;
  
        protected DynamicObject()
        {
            this.valuesStorage = new Dictionary<string,object>();
        }
  
        protected internal virtual T GetValue<T>(string propertyName)
        {
            object value;
            if (!this.valuesStorage.TryGetValue(propertyName, out value))
            {
                return default(T);
            }
            return (T) value;
        }
  
        protected internal virtual void SetValue<T>(string propertyName, T value)
        {
            this.valuesStorage[propertyName] = value;  
            this.RaisePropertyChanged(propertyName);
        }
  
        protected void RaisePropertyChanged(string propertyName)
        {
            this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }
  
        protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
        {
            var hanlder = this.PropertyChanged;
            if (hanlder != null)
            {
                hanlder(this, args);
            }
        }
    }
}


Scott
0
scott
Top achievements
Rank 1
answered on 28 Jul 2010, 04:10 PM
0
Hıncal
Top achievements
Rank 1
answered on 25 Aug 2010, 09:11 AM

I added below code to DynamicObject.cs file

public virtual T GetVal<T>(string propertyName)
        {
            object value;
            if (!this.valuesStorage.TryGetValue(propertyName, out value))
            {
                return default(T);
            }
            return (T)value;
        }

 

 

 

and then
 

 

 

var selectedItem = this.RadGridView1.SelectedItem as DynamicObject; 
var propertyValue = selectedItem.GetVal<int>("ID");

works again ;o)))

 

Tags
GridView
Asked by
scott
Top achievements
Rank 1
Answers by
Milan
Telerik team
scott
Top achievements
Rank 1
Hıncal
Top achievements
Rank 1
Share this question
or