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

Casting Grid Row item 's DataItem property to DataRowView

17 Answers 985 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Raj
Top achievements
Rank 1
Raj asked on 20 Mar 2009, 04:02 PM
Hi,

Couple of issues with 3.5 dll :
1. If we use the Telerik.Web.Ui.dll for framework 2.0 i.e. Bin/Telerik.Web.UI.dll, we are able to cast the e.Item.DataItem as DataRowView
(in ItemDataBound event handler) and access the values from the corresponding row in the datasource. But when we use the dll for 3.5 framework i.e. bin35/Telerik.Web.UI.dll , it doesn't allow the GridDataItem's  DataItem property to be cast as DataRowView. It shows it as a Dynamic Class. The interesting thing is it allows the GroupHeaderItems to be cast as DataRowView.
Can you please let me know , how to access the corresponding row Data values from the DataSource using dll for 3.5 framework.


2. Also, I have a GridCalculatedColumn which has a 'ISNULL' function in its expression and works fine with 2.0 dll. This same code gives error when it references the 3.5 dll.
e.g. 

 

<telerik:GridCalculatedColumn HeaderText="Name" UniqueName="df_nme" SortExpression="df_frst_nme"

 

 

DataType="System.String" DataFields="df_frst_nme, df_mid_nme, df_last_nme"

 

 

Expression='{2} + ",&nbsp;" + ISNULL({0},"") + "&nbsp;" + ISNULL({1},"")'>

 

 

</telerik:GridCalculatedColumn>

 



Thanks,

17 Answers, 1 is accepted

Sort by
0
Georgi Krustev
Telerik team
answered on 24 Mar 2009, 01:53 PM
Hello Raj,

RadGrid build upon .NET 3.5 uses Linq expressions. This is the reason why you get DataItem as a DynamicClass. If you need to get the properties of the DataItem, you can easily cast the DataItem to this dynamic class. Afterward you can access the required data using the corresponding property of the dynamic class. These properties have the same names as the column names from the data source.

In connection with the Calucated column:
Because of the usage of Linq expressions you cannot use IsNull() anymore. But you can easily check if the data is null, using String.IsNullOrEmpty.

Here is a code snippet showing how to achieve this:
                <telerik:GridCalculatedColumn HeaderText="CalculatedColumn" UniqueName="CalculatedColumn" 
                    DataType="System.String" DataFields="ProductName, ProductName, ProductName" 
                    Expression='{2} + ",&nbsp;" + (String.IsNullOrEmpty({0}) ? "" : {0}) + ",&nbsp;" + (String.IsNullOrEmpty({1}) ? "" : {1})'
                </telerik:GridCalculatedColumn> 
 

If you need to use the DataItem and Expression in the explained by you way, you should set EnableLinqExpressions to "false".

Regards, Georgi Krustev
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
Dathan
Top achievements
Rank 1
answered on 26 Mar 2009, 03:24 PM
Within the Linq expression, is there any way to call a custom method?  For example:

Expression="MyClass.MyMethod({0})"

This would enable some great customisation, but when I do this, I get a No property or field 'MyClass' exists in type 'DataRowView' error.

Is there any way to bring methods into scope of the Expression property?
0
Georgi Krustev
Telerik team
answered on 31 Mar 2009, 09:40 AM
Hello Dathan,

Unfortunately you cannot call methods in the Expression property, and actually this issue is not related with the Linq expressions. The function of the Expression property is to keep the format string, which later is used from the String.Format method. In order to format the string in the column, you should use only functionalities, which are allowed for use in the String.Format method.

Best regards,
Georgi Krustev
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
Dathan
Top achievements
Rank 1
answered on 31 Mar 2009, 05:26 PM
George, your reply seems to be at odds with your earlier post on this thread:

Expression='{2} + ",&nbsp;" + (String.IsNullOrEmpty({0}) ? "" : {0}) + ",&nbsp;" + (String.IsNullOrEmpty({1}) ? "" : {1})'

The above expression includes function calls to String.IsNullOrEmpty.  Likewise, I can in my own code make a call like this:

// With no {} tags, String.Format just returns the first parameter:  
String.Format(MyClass.MyMethod("data_field_value_here"));  

As long as MyMethod(object value) returns a string, it can be used this way.  The issue in looking at the code in Reflector is that there is no additional scope provided to the reflected call to String.Format in your code.  It would be a great if the project scope was available in the reflected call -- this would enable the functionality I described, and effectively would create a very extensible yet easy to use calculated column.

Interested in your thoughts on whether this might make its way in to your feature set, and I have to say: the grid itself is fantastic.
0
Georgi Krustev
Telerik team
answered on 02 Apr 2009, 04:18 PM
Hello Dathan,

I am sorry for the misunderstanding. The Linq expressions change the type of the of the data item to which the grid's row is bound. In other words if the Linq expressions are enabled the data item will be a Dynamic class and the columns from the DataRow are properties. Hence to check whether its property is null you cannot use IsNull, because this is SQL expression.

The other problem is that you cannot call a custom method in the string format expression, because it is out of the grid's scope.

To attain the functionality you are searching for you could use OnCustomAggregate event. For further information please review this online help topic. You can also examine this online demo.

Kind regards,
Georgi Krustev
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
Dathan
Top achievements
Rank 1
answered on 02 Apr 2009, 07:01 PM
Thanks for the clarification.  If the OnCustomAggregate method had a matching OnCustomCalculation method, I think we'd have a winner!  This would enable a custom method call for a cell value calculation.  Then, if your aggregate was available as well, you could either just sum these values, or a custom aggregate could be used as well.  Would be wonderful.
0
Georgi Krustev
Telerik team
answered on 06 Apr 2009, 09:13 AM
Hi Dathan,

Thank you for getting back to us.

Unfortunately the behavior you are trying to attain is not possible, because the grid does not have OnCustomCalculation event. To achieve your goal you can use ItemDataBound event. When it is raised you can perform the required calculation and then apply the modified text to the CalculatedColumn cell.

Here is a code snippet showing how to achieve this:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
   if(e.Item is GridDataItem) 
   { 
      var item = e.Item as GridDataItem; 
      //TODO perform calculation or the modification 
      //string result = item["FirstProperty"].toString() + //item["SecondProperty"].toString() + FormatText(item["SecondProperty"].toString()); 
      //item["CalculatedColumn1"].Text = result; 
   } 

Best regards,
Georgi Krustev
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
Dathan
Top achievements
Rank 1
answered on 06 Apr 2009, 02:32 PM
Sorry, I was not clear.  I was suggesting that having such an event would be a great feature addition in the future.  I'm quite aware it's not there today! 

I'm hoping it could make it in to a future release, as this would greatly simplify the coding required to deliver custom data fields into the grid without having to search out controls and plug values into them at data binding time.  This approach also would abstract the data from the delivery.
0
Georgi Krustev
Telerik team
answered on 06 Apr 2009, 05:06 PM
Hello Dathan,

Thank you for your suggestion. I will forward it to our developers for further discussion. If this appears to be a common request and a possible addition to the current grid object model, you may see it included in a future version of the product.

Kind regards,
Georgi Krustev
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
Raj
Top achievements
Rank 1
answered on 15 Apr 2009, 08:00 PM

Hi Georgi,

 

 

How can I cast the dataItem to DynamicClass.

 

If I say,

DynamicClass dc = dataItem as DynamicClass;

String memId = dc.MemberID;

 

then I will get compile error because MemberID is not a property of DynamicClass.

 

Can you please send me an example of casting dataItem to DynamicClass and using the column names as properties of this class.


Thanks,
Raj

0
Georgi Krustev
Telerik team
answered on 16 Apr 2009, 04:41 PM
Hello Raj,

I am sorry for not expressing myself clearly in the related post where the DynamicClass is mentioned. It is not possible to cast the e.Item.DataItem to a DymanicClass , because it is not a concrete type and as you mentioned the compiler cannot compile the code.

There are two possible solutions to get the DataItem in a comprehensive way:

1. You can retrieve the value for the columns which are declared in the DataKeyNames collection. Hence if you need to retrieve all values for the row, you should declare all columns in the DataKeyNames array.

Here is a code snippet showing what I mean:
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridDataItem) 
        { 
            var item = e.Item as GridDataItem; 
            int productID = Convert.ToInt32(item.GetDataKeyValue("ProductID")); 
 
            Hashtable values = new Hashtable(); 
            e.Item.OwnerTableView.ExtractValuesFromItem(values, item); 
            //with those two approaches the grid can retrieve only the columns which are declared in the DataKeyNames. 
        } 

2. The second approach is to use Reflection, which is not the best solution, because the Reflection is a slow process and will not work in medium trust.

Best regards,
Georgi Krustev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
CSurieux
Top achievements
Rank 2
answered on 19 Oct 2009, 05:35 PM
Hello,

Is there something new on this point, it looks like a regression in fonctionnalities.
We should be able to get directly our values from DataItem.

CS
0
Georgi Krustev
Telerik team
answered on 23 Oct 2009, 07:34 AM
Hello Christian,

If you do not use the Linq expressions, there is no problem to get the data from DataItem. Actually you can always retrieve the data from e.DataItem, but you should cast it to a concrete class, thus you will have intellisence and the code will compile.

Sincerely yours,
Georgi Krustev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
CSurieux
Top achievements
Rank 2
answered on 28 Oct 2009, 11:00 AM
Hello Georgi,

I don't use LinQ expressions but have added some calculated columns and it seems that you use linQ for this.
So I must change all my code and am unable to use dataitem anymore....

Regards
CS
0
Georgi Krustev
Telerik team
answered on 02 Nov 2009, 02:53 PM
Hello Christian,

The Linq expressions does not have influence under the inability to get DataItem. If the grid is bound to DataTable, the DataItem will be DataRowView and you can cast it to this object. If the grid is bound to a concrete object, you should be able to cast the DataItem to it.

If the problem is still exists, I will suggest you open a regular support ticket and send us a simple working test project. Thus we will review it locally and advice you further.

Sincerely yours,
Georgi Krustev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
CSurieux
Top achievements
Rank 2
answered on 02 Nov 2009, 03:00 PM
Hello Georgi,

Thanks for answer.
I am very busy now, I will do the sample later.
I am binding to a datatable and tracing the type of object inside dataitem.
It was datarowview until I added the calculated column and the sum/count in columns footer.
The type is now as I stated DynamicClass1....

Regards.

CS
0
Georgi Krustev
Telerik team
answered on 04 Nov 2009, 12:46 PM
Hello Christian,

Thank you for getting back to us.

Unfortunately I will be able to advise you further after I review the test project.

All the best,
Georgi Krustev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Grid
Asked by
Raj
Top achievements
Rank 1
Answers by
Georgi Krustev
Telerik team
Dathan
Top achievements
Rank 1
Raj
Top achievements
Rank 1
CSurieux
Top achievements
Rank 2
Share this question
or