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

How to access underlying row in ItemDataBound event

16 Answers 1691 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kermit
Top achievements
Rank 1
Kermit asked on 01 Aug 2012, 08:46 PM
How do I access the underlying datarow to which a GridDataItem is bound to in the ItemDataBound event?

I cannot use cell content because I am trying to get at a field in the datarow for which there is no column in the radgrid.

Thanks,
Kermit

16 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 02 Aug 2012, 05:33 AM
Hello,

I am not quite sure about your requirement. I suppose you want to access the binded DataField in ItemDataBound event.
aspx:
<%# Eval("EmployeeID")%>
C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
  if (e.Item is GridDataItem)
  {
     GridDataItem item = (GridDataItem)e.Item;
     string s = ((DataRowView)e.Item.DataItem)["EmployeeID"].ToString();
  }
}

Thanks,
Shinu.
0
Kermit
Top achievements
Rank 1
answered on 02 Aug 2012, 02:39 PM
Shinu, yes I do want to access the binded DataField in the ItemDataBound event. I use VB, not C#, so I converted your code and it cancelled due to "Unable to cast object of type 'DynamicClass1' to type 'System.Data.DataRowView'.  I dont think a dataitem can be cast as a datarowview, and I'm pretty sure that dataitem is a null object anyway when the ItemDataBound event handler is fired.

I'm not understanding why you declared an 'item' variable but did not use it.

Thanks,

Kermit
0
Shinu
Top achievements
Rank 2
answered on 03 Aug 2012, 05:29 AM
Hi,

Unfortunately I cannot replicate the error you are facing at my end. Here I am pasting the full code that I tried.
aspx:
<telerik:RadGrid ID="RadGrid1" DataSourceID="SqlDataSource1" AutoGenerateColumns="false" runat="server">
  <MasterTableView>
    <Columns>
     <telerik:GridTemplateColumn UniqueName="temp">
        <ItemTemplate>
          <%# Eval("EmployeeID")%>
        </ItemTemplate>
     </telerik:GridTemplateColumn>
    </Columns>
 </MasterTableView>
</telerik:RadGrid>
VB:
Private Sub RadGrid1_ItemDataBound(sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound
If TypeOf e.Item Is GridDataItem Then
           Dim s As String = DirectCast(e.Item.DataItem, DataRowView)("EmployeeID").ToString()
       End If
End Sub

Thanks,
Shinu.
0
Kermit
Top achievements
Rank 1
answered on 03 Aug 2012, 01:34 PM
Shinu,

Thanks for trying, but that does not work either.  e.item.dataitem is of the class "DynamicClass1" which cannot be DirectCast to the class "DataRowView".

I was mistaken in saying that dataitem was a null object when the ItemDataBound event handler is fired... I had read that somewhere else on this forum and presumed it to be true, but it is not.

Kermit
0
Adam Toth
Top achievements
Rank 1
answered on 14 Dec 2012, 10:13 AM
I would be interested in an answer to this - it looks like lots of people have asked in various Telerik forums but no working reply from Telerik yet.

As soon as you add an aggregate column to your Grid you lose the GridDataItem and get a DynamicClass1 and so lose the ability to easily determine the values in the row.  At the moment I think Im going to have to parse the string to get values...

Adam
0
Maria Ilieva
Telerik team
answered on 19 Dec 2012, 11:32 AM
Hi Adam,

The appraoch provided by Shinu in this thread works correctly on my side. Could you please specify what is your RadGrid settings and the exact issue you are facing so that we could further research on the problem?

All the best,
Maria Ilieva
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Adam Toth
Top achievements
Rank 1
answered on 19 Dec 2012, 12:02 PM
Hi Maria
The problem was pointed out by Kermit http://www.telerik.com/community/forums/aspnet-ajax/grid/how-to-access-underlying-row-in-itemdatabound-event.aspx#2215344

"Shinu,

Thanks for trying, but that does not work either.  e.item.dataitem is of the class "DynamicClass1" which cannot be DirectCast to the class "DataRowView"."


To replicate this you need to first add an aggregate column to your grid. Once you have done that you will see that the e.item.dataitem cannot be cast to a DataRowView as it is now a DynamicClass1 -  and so Shinu's code will not work,

Please give it a try.

Thanks

Adam
0
Maria Ilieva
Telerik team
answered on 21 Dec 2012, 02:40 PM
Hello Adam,

Please try to set the EnableLinqExpressions property of the RadGrid to false and verify if this helps..


Regards,
Maria Ilieva
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Adam Toth
Top achievements
Rank 1
answered on 21 Dec 2012, 04:29 PM
Hi Maria

I tried turning off EnableLinqExpressions and I get this runtime error:

[SyntaxErrorException: Syntax error: Missing operand before '=' operator.]
   System.Data.ExpressionParser.Parse() +4834792
   System.Data.DataExpression..ctor(DataTable table, String expression, Type type) +121
   System.Data.DataColumn.set_Expression(String value) +112
   Telerik.Web.UI.GridDataTableFromEnumerable.GetColumnsToUse() +2025
   Telerik.Web.UI.GridDataTableFromEnumerable.FillData() +1071
   Telerik.Web.UI.GridResolveEnumerable.Initialize() +55
   Telerik.Web.UI.GridResolveEnumerable.EnsureInitialized() +40
   Telerik.Web.UI.GridResolveEnumerable.get_DataTable() +31
   Telerik.Web.UI.GridEnumerableFromDataView..ctor(GridTableView owner, DataView dataView, Boolean autoGenerateColumns, GridColumnCollection presentColumns, String[] additionalField, Boolean retrieveAllFields, Boolean enableSplitHeaderText) +227
   Telerik.Web.UI.GridDataSourceHelper.CreateGridEnumerable(GridTableView owner, IEnumerable enumerable, Boolean caseSensitive, Boolean autoGenerateColumns, GridColumnCollection presentColumns, String[] additionalField, Boolean retrieveAllFields, Boolean enableSplitHeaderText) +101
   Telerik.Web.UI.GridDataSourceHelper.GetResolvedDataSource(GridTableView owner, Object dataSource, String dataMember, Boolean caseSensitive, Boolean autoGenerateColumns, GridColumnCollection presentColumns, String[] additionalField, Boolean retrieveAllFields, Boolean enableSplitHeaderText) +170
   Telerik.Web.UI.GridTableView.get_ResolvedDataSource() +234
   Telerik.Web.UI.GridTableView.CreateChildControls(IEnumerable dataSource, Boolean useDataSource) +73
   System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +57
   System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +114
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +31
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +142
   Telerik.Web.UI.GridTableView.PerformSelect() +38
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73
   Telerik.Web.UI.GridTableView.DataBind() +363
   Telerik.Web.UI.RadGrid.DataBind() +165
   Telerik.Web.UI.RadGrid.AutoDataBind(GridRebindReason rebindReason) +4058
   Telerik.Web.UI.RadGrid.OnLoad(EventArgs e) +177
   System.Web.UI.Control.LoadRecursive() +50
   System.Web.UI.Control.LoadRecursive() +141
   System.Web.UI.Control.LoadRecursive() +141
   System.Web.UI.Control.LoadRecursive() +141
   System.Web.UI.Control.LoadRecursive() +141
   System.Web.UI.Control.LoadRecursive() +141
   System.Web.UI.Control.LoadRecursive() +141
   System.Web.UI.Control.LoadRecursive() +141
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627
0
Shinu
Top achievements
Rank 2
answered on 26 Dec 2012, 09:10 AM
Hi,

I was able to replicate the error you are getting, when i added an aggregate column in the RadGrid. But i could resolve the issue by setting 'EnableLinqExpressions ' to false. Please provide the complete code and specify the Telerik version you are using to replicate the issue.

Thanks,
Shinu.
0
Adam Toth
Top achievements
Rank 1
answered on 02 Jan 2013, 10:39 AM
Hi Shinu

I guess what I'm trying to discover is a way of accessing the underlying column values when e.Item.DataItem is NOT a DataRowView but a DynamicClass1.

At the moment I am parsing the comma delimited string of values that you can get from the DynalicClass1 - this works but are you able to provide a better approach?

Thanks
Adam

ps
the Aggregate Columns are shown below and these are what cuase the error when you turn off EnableLinqExpressions

A SUM in the footer for 5 column Mon - Fri.

<telerik:GridNumericColumn DataField="Mon" UniqueName="Mon" Aggregate="Sum" FooterText=" "></telerik:GridNumericColumn>

AND a COlumn to SUM each row as follows:

<telerik:GridCalculatedColumn HeaderText="Total" DataType="System.Double" DataFields="Mon, Tue, Wed, Thu, Fri" Expression="(({0} == null) ? 0 : {0})  + (({1} == null) ? 0 : {1})  + (({2} == null) ? 0 : {2})  + (({3} == null) ? 0 : {3})  + (({4} == null) ? 0 : {4})" FooterText= " " Aggregate="Sum"></telerik:GridCalculatedColumn>

0
Shinu
Top achievements
Rank 2
answered on 03 Jan 2013, 05:19 AM
Hi,

As far as i know, one possible workaround for this issue is to prevent the null value from the Database using query and remove the null value checking in GridCalculatedColumn column's expression. We will be thankful if someone from telerik help us to solve the issue.

Thanks,
Shinu.
0
Justin
Top achievements
Rank 1
answered on 12 Dec 2013, 11:46 PM

I ran into similar situation, here's how I accessed the underlying data object's value using reflection.

e.Item.DataItem.GetType().GetProperty("WhateverYourPropertyNameIsOnYourObject").GetValue(e.Item.DataItem, null); 

 






0
Luca
Top achievements
Rank 2
answered on 16 Apr 2014, 03:17 PM
Maybe a bit late for an answer but i faced this problem today, i just cast the griddataitem.dataitem to dynamic type:

protected void RadGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
 if (e.Item is GridDataItem)
 {
  GridDataItem gridItem = e.Item as GridDataItem;
  dynamic dataItem = (dynamic)(gridItem.DataItem);
  gridItem.ToolTip = dataItem.ID + " - " + dataItem.UUID;
 }
}
0
Stephen
Top achievements
Rank 1
answered on 02 Mar 2016, 02:24 PM
This worked for me. Thanks so much.
0
Eyup
Telerik team
answered on 07 Mar 2016, 09:12 AM
Hi Stephen,

Please note that the dynamic keyword should be used with caution. I suggest that you try using the DataBinder.Eval() method to achieve this requirement:
http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/rows/accessing-cells-and-rows#accessing-raw-field-data-and-key-values

Regards,
Eyup
Telerik
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 Feedback Portal and vote to affect the priority of the items
Tags
Grid
Asked by
Kermit
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Kermit
Top achievements
Rank 1
Adam Toth
Top achievements
Rank 1
Maria Ilieva
Telerik team
Justin
Top achievements
Rank 1
Luca
Top achievements
Rank 2
Stephen
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or