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

Get Reference To A DetailsView column on the DetailTableDataBind

10 Answers 378 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tracy
Top achievements
Rank 1
Tracy asked on 31 Jan 2012, 04:14 AM
Hi

I have a RadGrid with Master and Detail views.  The load mode for the details view is HierarchLoadMode="ServerOnDemand".  I am binding the details view on the RadGrid DetailTableDataBind event.

I am trying to set the value of one of the detail fields to a value from the master table view but can't figure out how to get a reference to the detail row/field  being bound. 

 

Thank you for your assistance

Tracy

Private Sub rgvDataExceptions_DetailTableDataBind(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridDetailTableDataBindEventArgs) Handles rgvDataExceptions.DetailTableDataBind
        Dim SelectedRow As GridDataItem = CType(e.DetailTableView.ParentItem, GridDataItem)
        Dim intException As Int32 = SelectedRow.GetDataKeyValue("ExceptionID")
        Dim detailTable As GridTableView = DirectCast(SelectedRow.ChildItem.NestedTableViews(0), GridTableView)
        SQLDS_DataExceptionsMessage.SelectParameters.Clear()
        SQLDS_DataExceptionsMessage.SelectParameters.Add("intExceptionId", intException)
        e.DetailTableView.DataSource = SQLDS_DataExceptionsMessage
                  'Get value from master table row
        Dim strExceptionMessage As String = SelectedRow("ExceptionMessage").Text 

            I would then like to set the bltExceptionMessage,which is a bulled list that is in a template column of the details view

        
'Dim bltMessages As BulletedList = DirectCast(SelectedRow.FindControl("bltExceptionMessage"), BulletedList)
        'Dim aryMessages As New ArrayList
  
        'aryMessages.AddRange(Split(strExceptionMessage, "~"))
        'bltMessages.DataSource = aryMessages
        'bltMessages.DataBind()
  
  
    End Sub
 

 

10 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 31 Jan 2012, 05:06 AM
Hello Tracy,

You can refer controls in ItemDataBound event as shown below.
VB:
Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)
    If TypeOf e.Item Is GridDataItem AndAlso e.Item.OwnerTableView.Name = "Master" Then
        Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
        Dim list As BulletedList = DirectCast(item.FindControl("BulletedList1"), BulletedList)
    End If
End Sub

-Shinu.
0
Tracy
Top achievements
Rank 1
answered on 31 Jan 2012, 05:00 PM
Hi Shinu,

Thank you for your response.

The solution you gave me isn't working because the detail table is being bound on the rgvDataExceptions_DetailTableDataBind event and it doesn't seem to recognize the detail table when I try to reference it on the grid's item data bound event.  I am trying to set the value of a detail table  column to a value on the master table so wouldn't I need to set it on the DetailTableDataBind event since this is when the detail table is getting bound.

Private Sub rgvDataExceptions_DetailTableDataBind(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridDetailTableDataBindEventArgs) Handles rgvDataExceptions.DetailTableDataBind
       Dim SelectedRow As GridDataItem = CType(e.DetailTableView.ParentItem, GridDataItem)
       Dim intException As Int32 = SelectedRow.GetDataKeyValue("ExceptionID")
       Dim detailTable As GridTableView = DirectCast(SelectedRow.ChildItem.NestedTableViews(0), GridTableView)
       SQLDS_DataExceptionsMessage.SelectParameters.Clear()
       SQLDS_DataExceptionsMessage.SelectParameters.Add("intExceptionId", intException)
       e.DetailTableView.DataSource = SQLDS_DataExceptionsMessage
       Dim strExceptionMessage As String = SelectedRow("ExceptionMessage").Text
       'Dim bltMessages As BulletedList = DirectCast(SelectedRow.FindControl("bltExceptionMessage"), BulletedList)
       'Dim aryMessages As New ArrayList
       'aryMessages.AddRange(Split(strExceptionMessage, "~"))
       'bltMessages.DataSource = aryMessages
       'bltMessages.DataBind()
   End Sub

Thank you for you help.
0
Andrey
Telerik team
answered on 02 Feb 2012, 04:31 PM
Hello,

What particular problem do you have with your code, is some sort of exception is raised or you are getting different result?

You could check this help topic which discus how to bind detail table with OnDetailTableDataBind event.

Additionally, the better approach is to give the DetailTableView some name(with the Name property) and then to bind your bulleted list in the ItemDataBound event. In the event's body you should check if the currently binded item is from a TableView with name that matches the one you are seeking and if this condition is satisfied you could find then the bulleted list and to make the neseccary adjustments to it. You could use the following code as a base:

Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)
    If TypeOf e.Item Is GridDataItem AndAlso e.Item.OwnerTableView.Name = "DetailTableViewName" Then
        Dim selectedRow As GridDataItem = TryCast(e.Item, GridDataItem)
        Dim bltMessages As BulletedList = TryCast(selectedRow("ColumnUniqueName").FindControl("bltExceptionMessage"), BulletedList)
        Dim aryMessages As New ArrayList()
        aryMessages.AddRange(Split(strExceptionMessage, "~"))
        bltMessages.DataSource = aryMessages
        bltMessages.DataBind()
    End If
End Sub

You should change the "DetailTableViewName" with the name of the TableView object you have set and the same rule is applicable to the "ColumnUniqueName".

Give this approach a try and check whether you are getting the expected behavior. If you need further assistance, please share your RadGrid declaration, so all the people who want to help you to have better understanding of your code.

All the best,
Andrey
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
Tracy
Top achievements
Rank 1
answered on 02 Feb 2012, 05:05 PM
Hi Audrey,

My detail table does have a unique name assigned to it but my problem with retrieving a column from the detail on the gird's Item Databound event is that I am binding the detail table using ServerOnDemand, and the documentation indicates that the detail table isn't bound until the parent item is expanded. 

If HierarchyLoadMode is set to ServerOnDemand, the DetailTableDataBind event does not occur until the detail table's parent item is expanded.


If I put the following code in the grid's ItemDataBound it never fires, because the detail grid hasn't been bound.   That is why I am trying to set the detail column value on the DetailTableDataBind event.

Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)
    If TypeOf e.Item Is GridDataItem AndAlso e.Item.OwnerTableView.Name = "DetailTableViewName" Then
        Dim selectedRow As GridDataItem = TryCast(e.Item, GridDataItem)
        Dim bltMessages As BulletedList = TryCast(selectedRow("ColumnUniqueName").FindControl("bltExceptionMessage"), BulletedList)
        Dim aryMessages As New ArrayList()
        aryMessages.AddRange(Split(strExceptionMessage, "~"))
        bltMessages.DataSource = aryMessages
        bltMessages.DataBind()
    End If
End Sub


Below is the complete code for my page.  I appreciate any assistance you can give me.

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WBF SYS Data Exceptions.aspx.vb" Inherits="IPS_Gateway.WBF_SYS_Data_Exceptions" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="act" %>
<%@ Register TagPrefix="wuc" TagName="PageHeader" Src="~/User Controls/WUC Page Heading.ascx" %>
  
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  
<head id="Head1" runat="server">
    <title></title>
  
</head>
<body>
    <form id="form1" runat="server">
    <act:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"/>
    <telerik:RadWindowManager ID="rwmManager" runat="server"></telerik:RadWindowManager>  
  
    <asp:SqlDataSource ID="SQLDS_DataExceptions"        runat="server"  ConnectionString="<%$ ConnectionStrings:IPSDataConnectionString %>"  SelectCommandType="StoredProcedure" SelectCommand="[System].[DSP_DataExceptions-Sel]" />                                
    <asp:SqlDataSource ID="SQLDS_DataExceptionsMessage" runat="server"  ConnectionString="<%$ ConnectionStrings:IPSDataConnectionString %>"  SelectCommandType="StoredProcedure" SelectCommand="[System].[DSP_DataExceptions-Sel]" />                                
  
   <asp:panel ID="pnlPage"   runat="server" cssClass="css_GPC01_Panel_MainContent">      
    <wuc:PageHeader id="wucPageHeader" runat="server"></wuc:PageHeader>                   
  
        <asp:Panel ID="pnlPageContent"                      runat="server"  cssClass="css_GPC01_Panel_PageContent" >     
            <asp:UpdatePanel ID="uppUpdateList"             runat="server"  ChildrenAsTriggers="true">
                <ContentTemplate>
                  <asp:Timer ID="tmrImportStatus"           runat="server"  Interval="5000" ></asp:Timer
                    <telerik:RadGrid ID="rgvDataExceptions"   runat="server"  DataSourceID="SQLDS_DataExceptions"    
                                         EnableEmbeddedSkins="true"         Skin="Office2010Silver"         CssClass="css_GRD01_Grid_Master"    Height="560px"  Width="1230px"
                                         EnableViewState="true"             AutoGenerateColumns="false"     AllowMultiRowSelection="false"      AllowFilteringByColumn="true" 
                                         AllowSorting="true"                EnableLinqExpressions="false"   AllowPaging="true"                 PageSize="50">                                  
                            <ItemStyle   Wrap="false" />
                            <PagerStyle AlwaysVisible="true"    Mode="NextPrevNumericAndAdvanced"/>
                            <ClientSettings AllowColumnsReorder="false" EnablePostBackOnRowClick="true" AllowExpandCollapse="true" EnableRowHoverStyle="true"    >                                  
                                <Selecting      AllowRowSelect="true" />
                                <Scrolling      AllowScroll="true"      UseStaticHeaders="true" />                        
                        </ClientSettings>
                        <MasterTableView DataSourceID="SQLDS_DataExceptions"  DataKeyNames="ExceptionId" Name="Exceptions" >
                            <Columns>
                                <telerik:GridBoundColumn DataField="ExceptionId"        UniqueName="ExceptionID"        HeaderText="Id"     Display="false"/>
                                <telerik:GridBoundColumn DataField="ExceptionSource"    UniqueName="ExceptionSource"    HeaderText="Source"             HeaderStyle-HorizontalAlign="Left"   HeaderStyle-Width="100px"/>
                                <telerik:GridBoundColumn DataField="ExceptionType"      UniqueName="ExceptionType"      HeaderText="Type"               HeaderStyle-HorizontalAlign="Center" HeaderStyle-Width="80px" ItemStyle-HorizontalAlign="Left"  />
                                <telerik:GridBoundColumn DataField="ExceptionError"     UniqueName="ExceptionError"     HeaderText="Error"              HeaderStyle-HorizontalAlign="Center"  HeaderStyle-Width="175px"  ItemStyle-HorizontalAlign="Left"    />
                                <telerik:GridBoundColumn DataField="ExceptionKeyId"     UniqueName="ExceptionKeyId"     HeaderText="Key Id"             HeaderStyle-HorizontalAlign="Center" HeaderStyle-Width="200px" ItemStyle-HorizontalAlign="Left"  />
                                <telerik:GridBoundColumn DataField="ExceptionMessage"   UniqueName="ExceptionMessageDisplay"   HeaderText="Exception Message"  HeaderStyle-HorizontalAlign="Center"  HeaderStyle-Width="430px"  ItemStyle-HorizontalAlign="Left"     />
                                <telerik:GridBoundColumn DataField="ExceptionMessage"   UniqueName="ExceptionMessage"   HeaderText="Exception Message"  Visible="false" />
                                <telerik:GridBoundColumn DataField="CreatedBy"          UniqueName="CreatedBy"          HeaderText="Created By"         HeaderStyle-HorizontalAlign="Left"   HeaderStyle-Width="120px"/>
                                <telerik:GridBoundColumn DataField="CreatedDate"        UniqueName="CreatedDate"        HeaderText="Created Date"       HeaderStyle-HorizontalAlign="Center" HeaderStyle-Width="120px"  ItemStyle-HorizontalAlign="Left"  DataType="System.DateTime" DataFormatString="{0:MM/dd/yy h:mm:ss t}"   />
                            </Columns>
                            <DetailTables>
                                   <telerik:GridTableView   DataKeyNames="ExceptionId"               HierarchyLoadMode="ServerOnDemand"    Name="dtlExceptions"  
                                                             CommandItemDisplay="None"
                                                            cssClass="css_GRD01_Grid_Detail"      Width="1160px"                      PageSize="500" >
                                        <Columns>
                                             <telerik:GridTemplateColumn     UniqueName="gtcExceptionMessage"           HeaderText="ErrorMessage"         HeaderStyle-Width="1120px"     HeaderStyle-HorizontalAlign="Center"    ItemStyle-HorizontalAlign="Left"  >
                                                <ItemTemplate >
                                                              <asp:BulletedList ID="bltExceptionMessage"      runat="server"   BulletStyle="Square"  Height="300px"   />
                                                </ItemTemplate>   
                                            </telerik:GridTemplateColumn>                                 
                                                                                
                                            <telerik:GridBoundColumn        DataField="ExceptionID"          UniqueName="ExceptionIdDetail"    Visible="false" DataType="System.Int32" />
                                           </Columns>
   
                                    </telerik:GridTableView
                                </DetailTables>                               
                        </MasterTableView>                    
                    </telerik:RadGrid>
                </ContentTemplate>
            </asp:UpdatePanel>    
        </asp:Panel>
    </asp:panel
</form>
</body>
</html>

Imports Telerik.Web.UI
Partial Public Class WBF_SYS_Data_Exceptions
    Inherits System.Web.UI.Page
  
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Not IsPostBack Then
                Me.wucPageHeader.prpPageParameters = If(Request("PageParameters") = Nothing, "Unknown, Unknown, 0,1,1,1", Request("PageParameters"))
            End If
        End If
    End Sub
  
    Private Sub rgvDataExceptions_DetailTableDataBind(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridDetailTableDataBindEventArgs) Handles rgvDataExceptions.DetailTableDataBind
        Dim SelectedRow As GridDataItem = CType(e.DetailTableView.ParentItem, GridDataItem)
        Dim intException As Int32 = SelectedRow.GetDataKeyValue("ExceptionID")
        Dim detailTable As GridTableView = DirectCast(SelectedRow.ChildItem.NestedTableViews(0), GridTableView)
        SQLDS_DataExceptionsMessage.SelectParameters.Clear()
        SQLDS_DataExceptionsMessage.SelectParameters.Add("intExceptionId", intException)
        e.DetailTableView.DataSource = SQLDS_DataExceptionsMessage
  
        Dim strExceptionMessage As String = SelectedRow("ExceptionMessage").Text
        'Dim bltMessages As BulletedList = DirectCast(SelectedRow.FindControl("bltExceptionMessage"), BulletedList)
        'Dim aryMessages As New ArrayList
  
        'aryMessages.AddRange(Split(strExceptionMessage, "~"))
        'bltMessages.DataSource = aryMessages
        'bltMessages.DataBind()
  
  
    End Sub
  
    Private Sub rgvDataExceptions_ItemCommand(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles rgvDataExceptions.ItemCommand
           Select e.CommandName
            Case RadGrid.ExpandCollapseCommandName
                Dim item As GridDataItem = TryCast(e.Item, GridDataItem)
  
                If Not e.Item.Expanded Then
                    'Code to set detail colum
                End If
        End Select
    End Sub
  
    Private Sub rgvDataExceptions_ItemCreated(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles rgvDataExceptions.ItemCreated
        If TypeOf e.Item Is GridNestedViewItem Then
            Dim nestedItem As GridNestedViewItem = TryCast(e.Item, GridNestedViewItem)
            AddHandler nestedItem.NestedViewCell.PreRender, AddressOf NestedViewCell_PreRender
        End If
  
    End Sub
  
    Private Sub rgvDataExceptions_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles rgvDataExceptions.ItemDataBound
        Dim strExceptionMessage As String
        If TypeOf e.Item Is GridDataItem And e.Item.OwnerTableView.Name = "Exceptions" Then
            Dim grdItem As GridDataItem = DirectCast(e.Item, GridDataItem)
            strExceptionMessage = grdItem("ExceptionMessage").Text
  
            'If the width of the following fields is greater than the length of the column then set the tooltip text to
            'the entire field value and truncate the actual field to only show 25 characters and ...
            If grdItem("ExceptionMessageDisplay").Text.Length > 70 Then
                grdItem("ExceptionMessageDisplay").ToolTip = grdItem("ExceptionMessageDisplay").Text.ToString()
                grdItem("ExceptionMessageDisplay").Text = (grdItem("ExceptionMessageDisplay").Text).Substring(0, 70) + "... "
            End If
            If grdItem("ExceptionKeyId").Text.Length > 100 Then
                grdItem("ExceptionKeyId").ToolTip = grdItem("ExceptionKeyId").Text.ToString()
                grdItem("ExceptionKeyId").Text = (grdItem("ExceptionKeyId").Text).Substring(0, 10) + "... "
            End If
        End If
  
        '*** This code never fires ***
        If TypeOf e.Item Is GridDataItem And e.Item.OwnerTableView.Name = "dtlExceptions" Then
  
            Dim dtlItem As GridDataItem = DirectCast(e.Item, GridDataItem)
            Dim bltMessages As BulletedList = DirectCast(dtlItem.FindControl("bltExceptionMessage"), BulletedList)
            Dim aryMessages As New ArrayList
  
            aryMessages.AddRange(Split(strExceptionMessage, "~"))
            bltMessages.DataSource = aryMessages
            bltMessages.DataBind()
        End If
    End Sub
  
    Private Sub NestedViewCell_PreRender(ByVal sender As Object, ByVal e As EventArgs)
        DirectCast(sender, Control).Controls(0).SetRenderMethodDelegate(New RenderMethod(AddressOf Me.NestedViewTable_Render))
    End Sub
  
    Protected Sub NestedViewTable_Render(ByVal writer As HtmlTextWriter, ByVal control As Control)
        'Set properties for Detail Table View
        control.SetRenderMethodDelegate(Nothing)
        writer.Write("<div style='height: 350px; width: 1170px; overflow: auto;'>")
        control.RenderControl(writer)
        writer.Write("</div>")
  
    End Sub
End Class

0
Shinu
Top achievements
Rank 2
answered on 03 Feb 2012, 05:29 AM
Hello Tracy,

After examining your code, I cannot reproduce exactly how you are attaching the events. Make sure that you have attached the events properly.
aspx:
<telerik:RadGrid runat="server" ID="RadGrid2" AutoGenerateColumns="False" OnNeedDataSource="RadGrid2_NeedDataSource"
OnDetailTableDataBind="RadGrid2_DetailTableDataBind" OnPreRender="RadGrid2_PreRender"  onitemdatabound="RadGrid2_ItemDataBound" AutoGenerateEditColumn="true">
</telerik:RadGrid>

-Shinu.
0
Andrey
Telerik team
answered on 03 Feb 2012, 03:28 PM
Hello,

DetailTableDataBind event is too early in the control life-cycle and that is why you could not access neither the columns nor the rows of the TableView at this stage, they are not yet created.

Nested items are created as soon as the parent item is expanded. You could check this help topic for more information about hierarchy structure of RadGrid.

Why you are trying to assign a value to some row/column so early in the life-cycle of the control? You could change controls value until PreRender stage including.

If you are more specific about your requirements we will be able to give you more to the point directions.

All the best,
Andrey
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
Tracy
Top achievements
Rank 1
answered on 06 Feb 2012, 03:58 AM
Hi Audrey,

What I am trying to do is bind a bulleted list that is in the detail table to a value from the expanded master table.

Thank You
0
Andrey
Telerik team
answered on 06 Feb 2012, 04:17 PM
Hello,

The approach that I have suggested you in my previous post will do the job of binding the BulletedList control in the ItemDataBound event of RadGrid.

It does not matter that it is not available on Initial page load, what does it matters is that it will be available when the parent item is expanded.

I have created a sample project to illustrate the approach. Give the project a try and check whether this is the desired behavior.

Kind regards,
Andrey
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Andrey
Telerik team
answered on 06 Feb 2012, 04:17 PM
Hello,

The approach that I have suggested you in my previous post will do the job of binding the BulletedList control in the ItemDataBound event of RadGrid.

It does not matter that it is not available on Initial page load, what does it matters is that it will be available when the parent item is expanded.

I have created a sample project to illustrate the approach. Give the project a try and check whether this is the desired behavior.

Kind regards,
Andrey
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Andrey
Telerik team
answered on 06 Feb 2012, 04:19 PM
Hello,

The approach I suggested you in one of my previous post will do the job for binding the BulletedList control in the ItemDataBound event of RadGrid.

It does not matter that it is not available on Initial page load, what does it matters is that it will be available when the parent item is expanded.

I have created a sample project to illustrate the approach. Give the project a try and check whether this is the desired behavior.

Kind regards,
Andrey
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Tags
Grid
Asked by
Tracy
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Tracy
Top achievements
Rank 1
Andrey
Telerik team
Share this question
or