How to access underlying row in ItemDataBound event

17 posts, 0 answers
  1. Kermit
    Kermit avatar
    20 posts
    Member since:
    Jan 2012

    Posted 01 Aug 2012 Link to this post

    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
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 02 Aug 2012 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Kermit
    Kermit avatar
    20 posts
    Member since:
    Jan 2012

    Posted 02 Aug 2012 Link to this post

    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
  5. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 03 Aug 2012 Link to this post

    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.
  6. Kermit
    Kermit avatar
    20 posts
    Member since:
    Jan 2012

    Posted 03 Aug 2012 Link to this post

    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
  7. Adam Toth
    Adam Toth avatar
    11 posts
    Member since:
    Sep 2006

    Posted 14 Dec 2012 Link to this post

    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
  8. Maria Ilieva
    Admin
    Maria Ilieva avatar
    4017 posts

    Posted 19 Dec 2012 Link to this post

    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.
  9. Adam Toth
    Adam Toth avatar
    11 posts
    Member since:
    Sep 2006

    Posted 19 Dec 2012 Link to this post

    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
  10. Maria Ilieva
    Admin
    Maria Ilieva avatar
    4017 posts

    Posted 21 Dec 2012 Link to this post

    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.
  11. Adam Toth
    Adam Toth avatar
    11 posts
    Member since:
    Sep 2006

    Posted 21 Dec 2012 Link to this post

    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
  12. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 26 Dec 2012 Link to this post

    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.
  13. Adam Toth
    Adam Toth avatar
    11 posts
    Member since:
    Sep 2006

    Posted 02 Jan 2013 Link to this post

    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>

  14. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 02 Jan 2013 Link to this post

    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.
  15. Justin
    Justin avatar
    9 posts
    Member since:
    Dec 2013

    Posted 12 Dec 2013 Link to this post

    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); 

     






  16. Luca
    Luca avatar
    5 posts
    Member since:
    Jul 2012

    Posted 16 Apr 2014 Link to this post

    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;
     }
    }
  17. Stephen
    Stephen avatar
    1 posts
    Member since:
    Jan 2013

    Posted 02 Mar in reply to Luca Link to this post

    This worked for me. Thanks so much.
  18. Eyup
    Admin
    Eyup avatar
    3005 posts

    Posted 07 Mar Link to this post

    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
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017