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

determine object type in DataRecord

2 Answers 109 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Scott
Top achievements
Rank 1
Scott asked on 12 Dec 2008, 07:56 PM
In this code:

Customer cutomer = (Customer) (record as DataRecord).Data;  
cutomer.HasOrders = true;  

I am casting record as a Customer.  What if I don't know that my RadGrid is bound to Customer objects?  Is there a way to tell what type of objects are bound to my radgrid and then cast out each record in order to get access to the properties?

Thanks,
Scott



2 Answers, 1 is accepted

Sort by
0
Christopher
Top achievements
Rank 1
answered on 12 Dec 2008, 08:50 PM
Scott,
   You could call the GetType() method on the Data property to determine the type of object stored in it.  However from there I think your looking at a switch block or the like to do your individual casts and operations on the object.

Chris
0
Milan
Telerik team
answered on 15 Dec 2008, 09:32 AM
Hi Scott,

As christopher suggested the easiest way to do that is to use the GetType method on the Data property to get the type of your data record. After that can use if statements to determine which kind of object you are dealing with. The code will look something like that:

Type dataType = dataRecord.Data.GetType();  
 
if (dataType == typeof(Customer))  
{  
    // Customers pecific code  
}  
 
if (dataType == typeof(Order))   
{  
    // Order specific code  
}  
 
// ...  

If you are a fan of switch statements you won't be able to use them in this scenario since you need to have an integral type (string, int, etc) and the Type object does not fall into this category.

I hope this helps.

Greetings,
Milan
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
GridView
Asked by
Scott
Top achievements
Rank 1
Answers by
Christopher
Top achievements
Rank 1
Milan
Telerik team
Share this question
or