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

RAD Grid - ItemDataBound event for detail tables

11 Answers 2027 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Riz
Top achievements
Rank 1
Riz asked on 19 Sep 2011, 06:34 AM
Hi,

We are using Rad Grid which has a detail table per row in master table. We have an event ItemDataBound for master table but can't find this event for child/detail table. How can I use that?

Thanks

11 Answers, 1 is accepted

Sort by
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 19 Sep 2011, 06:46 AM
Hello,

there is only one itemdatabound is there for all grid. You can identify parent / child grid by using below code snippet..

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem && e.Item.OwnerTableView.Name == "ParentGrid")
        {
            // your logic should come here
        }
        else if (e.Item is GridDataItem && e.Item.OwnerTableView.Name == "ChildGrid")
        {
            // your logic should come here
        }
    }
<MasterTableView  Name="ParentGrid">
           <DetailTables>
            <telerik:GridTableView Name="ChildGrid"


Thanks,
Jayesh Goyani
0
Riz
Top achievements
Rank 1
answered on 19 Sep 2011, 10:57 AM
0
Kapil
Top achievements
Rank 1
answered on 10 Jul 2013, 02:26 PM
I am able to get Client Grid , but value are coming as "nbsp;"

protected void Grid_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem && e.Item.OwnerTableView.Name == "ChildGrid")
            {
                GridDataItem item = (GridDataItem)e.Item;
                string val1 = item["PublicationType"].Text;
            }
        }


here val1 is coming as "nbsp;"

where as when grid get displayed it is displaying the actual value as expected. but I am not getting value in the above ItemDataBound function
0
Jayesh Goyani
Top achievements
Rank 2
answered on 11 Jul 2013, 11:13 AM
Hello,

Can you please provide your grid mark up (aspx page code)?

Thanks,
Jayesh Goyani
0
Kapil
Top achievements
Rank 1
answered on 11 Jul 2013, 01:11 PM
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Submissions.ascx.cs"
    Inherits="SitefinityWebApp.Controls.Submissions" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<asp:PlaceHolder ID="PlaceHolder" runat="server"></asp:PlaceHolder>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using Telerik.Sitefinity.DynamicModules;
using Telerik.Sitefinity.DynamicModules.Model;
using Telerik.Sitefinity.Model;
using Telerik.Sitefinity.Utilities.TypeConverters;
using Telerik.Web.UI;
using Telerik.Sitefinity.Modules.Libraries;
using Telerik.Sitefinity.Model.ContentLinks;
using Telerik.Sitefinity.Libraries.Model;
 
namespace SitefinityWebApp.Controls
{
    [Telerik.Sitefinity.Web.UI.ControlDesign.ControlDesigner(typeof(SitefinityWebApp.Designer.SubmissionDesigner))]
    public partial class Submissions : System.Web.UI.UserControl
    {
        private string rowselected;
 
        public string DocumentStatus { get; set; }
        public string Sector { get; set; }
        public string Series { get; set; }
        public string PublicationType { get; set; }
 
        protected void Grid_DetailTableDataBind(object source, Telerik.Web.UI.GridDetailTableDataBindEventArgs e)
        {
            e.DetailTableView.DataSource = GetGridItems();
        }
 
        private IQueryable<DynamicContent> GetGridItems()
        {
            Type submissionsType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.DMS.Submission");
 
            DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
            var myCollection = dynamicModuleManager.GetDataItems(submissionsType)
                                                    .Where(i => i.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live &&
                                                                i.Visible == true && i.GetValue<string>("Sector") == Sector);
            return myCollection;
        }
 
         
        private void DefineGridStructure(RadGrid grid)
        {
            Dictionary<string, string> columnNamesMainGrid;
            Dictionary<string, string> columnNamesChildGrid;
 
            columnNamesMainGrid = new Dictionary<string, string>();
            columnNamesMainGrid.Add("DocumentName", "Name");
            columnNamesMainGrid.Add("Document", "Document");
            columnNamesMainGrid.Add("DocumentStatus", "Document Status");
 
            columnNamesChildGrid = new Dictionary<string, string>();
            columnNamesChildGrid.Add("Sector", "~/Sector?");
            columnNamesChildGrid.Add("Series", "~/Series?");
            columnNamesChildGrid.Add("PublicationType", "~/publicationtype?");
            columnNamesChildGrid.Add("NumberOfPages", "Number Of Pages");
            columnNamesChildGrid.Add("Document", "~/Document?");
 
 
            grid.ID = "Grid";
            grid.MasterTableView.Name = "MasterGrid";
            grid.MasterTableView.DataKeyNames = new string[] { "ID" };
 
            grid.Width = Unit.Percentage(100);
            grid.PageSize = 10;
            grid.AllowPaging = true;
            grid.AllowSorting = true;
            grid.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;
            grid.AutoGenerateColumns = false;
            grid.ShowStatusBar = false;
            grid.ShowHeader = true;
             
            grid.ClientSettings.AllowExpandCollapse = true;
 
            grid.DetailTableDataBind += new GridDetailTableDataBindEventHandler(Grid_DetailTableDataBind);
            grid.MasterTableView.PageSize = 10;
            grid.ItemDataBound += new GridItemEventHandler(Grid_ItemDataBound);
            GridBoundColumn boundColumn;
            GridHyperLinkColumn hyperlinkColumn = new GridHyperLinkColumn();
 
            foreach (KeyValuePair<string, string> column in columnNamesMainGrid)
            {
                boundColumn = new GridBoundColumn();
                boundColumn.DataField = column.Key;
                boundColumn.HeaderText = column.Value;
                grid.MasterTableView.Columns.Add(boundColumn);
 
            }
 
            boundColumn = new GridBoundColumn();
            boundColumn.DataField = "ID";
            boundColumn.HeaderText = "ID";
            boundColumn.Visible = false;
            grid.MasterTableView.Columns.Add(boundColumn);
 
            GridTableView tableViewChild = new GridTableView(grid);
            tableViewChild.Width = Unit.Percentage(100);
            tableViewChild.DataKeyNames = new string[] { "ID" };
            tableViewChild.Name = "ChildGrid";
            tableViewChild.ShowHeader = false;
            tableViewChild.GridLines = GridLines.None;
            tableViewChild.BorderStyle = BorderStyle.None;
 
             
            foreach (KeyValuePair<string, string> column in columnNamesChildGrid)
            {
                if (column.Value.StartsWith("~"))
                {
                    hyperlinkColumn = new GridHyperLinkColumn();
                    hyperlinkColumn.DataTextField = column.Key;
                    hyperlinkColumn.UniqueName = column.Key;
                    hyperlinkColumn.NavigateUrl = column.Value;
                    tableViewChild.Columns.Add(hyperlinkColumn);
                }
                else
                {
                    boundColumn = new GridBoundColumn();
                    boundColumn.DataField = column.Key;
                    boundColumn.HeaderText = column.Value;
                    tableViewChild.Columns.Add(boundColumn);
                }
            }
            GridRelationFields relationFields = new GridRelationFields();
            relationFields.MasterKeyField = "ID";
            relationFields.DetailKeyField = "ID";
            tableViewChild.ParentTableRelation.Add(relationFields);
             
            grid.MasterTableView.DetailTables.Add(tableViewChild);
            grid.MasterTableView.HierarchyLoadMode = GridChildLoadMode.Client;
        }
        protected void Grid_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem && e.Item.OwnerTableView.Name == "ChildGrid")
            {
                GridDataItem item = (GridDataItem)e.Item;
                string val1 = item["PublicationType"].Text;
                HyperLink hLink = (HyperLink)item["PublicationType"].Controls[0];
                hLink.NavigateUrl += val1;
            }
        }
        private void DefineGrid()
        {
            var myCollection = GetGridItems();
            RadGrid grid = new RadGrid();
            grid.DataSource = myCollection;
            DefineGridStructure(grid);
            this.PlaceHolder.Controls.Add(grid);
        }
 
 
 
        protected void Page_Init(object source, System.EventArgs e)
        {
            DefineGrid();
        }
    }
}
0
Kapil
Top achievements
Rank 1
answered on 11 Jul 2013, 01:14 PM
this is our requirement as we are getting "Telerik.Sitefinity.DynamicTypes.Model.DMS.Submission" from sharepoint connector.
0
Maria Ilieva
Telerik team
answered on 15 Jul 2013, 12:29 PM
Hello Kapil,

I suppose that the issue you are facing is related to the Breaking Change introduced in the previous release, in which in order to approve the RadGrid performance the invisible columns state is no longer persisted. See the sticky thread below for more information on this matter:
http://www.telerik.com/community/forums/aspnet-ajax/grid/breaking-change-hidden-column-cell-text-is-not-persisted-in-viewstate.aspx

Regards,
Maria Ilieva
Telerik
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 the blog feed now.
0
Paulo
Top achievements
Rank 1
answered on 22 Jul 2013, 12:30 PM
Thx Jayesh Goyani!
You solved my problem.
[]'s.
0
Perry Blanchard
Top achievements
Rank 1
answered on 05 Jun 2015, 02:40 PM

I try to hide/display three items in <detailTable> by useing RadGrid1_ItemDataBound depend ParentTable item DepartmentID .

Looks like all three items not work right, Sometimes  working right if you expand/collapse one item few times  and sometimes make all wrong if you expand one item while another item is still expand and ........

My three items,

1, detailTable.GetColumn("EditCommandColumn2").Display = True/False
2, detailTable.GetColumn("DeleteColumn2").Display = True/False
3, detailTable.CommandItemSettings.ShowAddNewRecordButton = True/False

My  part of aspx page like this,

............

<telerik:RadGrid ID="RadGridDepartment" runat="server" AllowAutomaticDeletes="True" AllowAutomaticInserts="True" AllowAutomaticUpdates="True" AllowMultiRowSelection="False" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataSourceID="SqlDataSourceDepartment" GridLines="None" PageSize="40" ShowStatusBar="true" Skin="Outlook">
<PagerStyle Mode="NumericPages" />
<MasterTableView runat="server" AllowMultiColumnSorting="True" AutoGenerateColumns="False" CommandItemDisplay="Top" CommandItemSettings-AddNewRecordText="Add New Department" CommandItemSettings-ShowRefreshButton="false" DataKeyNames="DepartmentID" DataSourceID="SqlDataSourceDepartment" EditMode="EditForms" Name="Department" Width="100%">
<CommandItemSettings ShowAddNewRecordButton="false" />
<DetailTables>
<telerik:GridTableView runat="server" AutoGenerateColumns="False" CommandItemDisplay="Top" CommandItemSettings-AddNewRecordText="Add New Communication" CommandItemSettings-ShowRefreshButton="false" DataKeyNames="DepartmentID,CommunicationID" DataSourceID="SqlDataSourceCommunications" EditMode="EditForms" Name="Communications" Width="100%">

<ParentTableRelation>
<telerik:GridRelationFields DetailKeyField="DepartmentID" MasterKeyField="DepartmentID" />
</ParentTableRelation>
<HeaderStyle CssClass="InnerHeaderStyle" />
<ItemStyle CssClass="InnerItemStyle" />
<AlternatingItemStyle CssClass="InnerAlernatingItemStyle" />
<Columns>
<telerik:GridEditCommandColumn ButtonType="ImageButton" EditImageUrl="images/editChilden.png" HeaderText="Edit" UniqueName="EditCommandColumn2">
<HeaderStyle Width="20px" />
<ItemStyle CssClass="MyImageButton" />
</telerik:GridEditCommandColumn>
<telerik:GridBoundColumn UniqueName="CommunicationID" HeaderText="Communication ID"
DataField="CommunicationID" DataType="System.Int32"
FilterControlAltText="Filter Communication ID column" ReadOnly="True" Visible="false"
AllowSorting="False">
</telerik:GridBoundColumn>

<telerik:GridButtonColumn ButtonType="ImageButton" CommandName="Delete" ConfirmText="Delete this communication?" HeaderText="Delete" ImageUrl="~/images/deleteChilden2.png" Text="Delete" UniqueName="DeleteColumn2">
<HeaderStyle Width="20px" />
<ItemStyle CssClass="MyImageButton" HorizontalAlign="Center" />
</telerik:GridButtonColumn>
</Columns>
<SortExpressions>
<telerik:GridSortExpression FieldName="CommunicationID" SortOrder="Ascending" />
</SortExpressions>
<EditFormSettings CaptionDataField="CommunicationID" CaptionFormatString="Edit Communication ID = {0}" ColumnNumber="2" EditColumn-ButtonType="PushButton" FormCaptionStyle-ForeColor="#990000" InsertCaption="Add New Communications">
<FormTableStyle CellPadding="2" CellSpacing="0" CssClass="module" GridLines="None" Height="90px" Width="100%" />
<EditColumn CancelText="Cancel" UniqueName="EditCommandColumn1" UpdateText="Update">
</EditColumn>
<FormTableButtonRowStyle CssClass="EditFormButtonRow" HorizontalAlign="Left" />
</EditFormSettings>
</telerik:GridTableView>
</DetailTables>

<Columns>

<telerik:GridBoundColumn UniqueName="DepartmentID" HeaderText="Department ID"
DataField="DepartmentID" DataType="System.Int32"
FilterControlAltText="Filter Department ID column" ReadOnly="True" HeaderStyle-Width="100px"
AllowSorting="False">
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn HeaderText="Department" UniqueName="TemplateColumnDepartment" SortExpression="Department">
<EditItemTemplate>
<asp:TextBox ID="TextBoxDepartment" runat="server" Text='<%# Bind("Department")%>' Width="800"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ControlToValidate="TextBoxDepartment" ErrorMessage="* required" ForeColor="Red">
</asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LabelDepartment" runat="server" Text='<%# Eval("Department")%>'></asp:Label>
</ItemTemplate>
</telerik:GridTemplateColumn>
                        
</Columns>

<SortExpressions>
<telerik:GridSortExpression FieldName="DepartmentID" SortOrder="Ascending" />
</SortExpressions>
<EditFormSettings CaptionDataField="DepartmentID" CaptionFormatString="Edit Department ID = {0}" ColumnNumber="2" EditColumn-ButtonType="PushButton" FormCaptionStyle-ForeColor="#990000" InsertCaption="Add New Department">
<FormTableStyle CellPadding="2" CellSpacing="0" CssClass="module" GridLines="None" Height="90px" Width="100%" />
<EditColumn CancelText="Cancel" UniqueName="EditCommandColumn1" UpdateText="Update">
</EditColumn>
<FormTableButtonRowStyle CssClass="EditFormButtonRow" HorizontalAlign="Left" />
</EditFormSettings>
</MasterTableView>
</telerik:RadGrid>

 

 ................

My aspx.vb like this.

Private Sub RadGridDepartment_ItemDataBound(sender As Object, e As GridItemEventArgs) Handles RadGridDepartment.ItemDataBound
        If TypeOf e.Item Is GridDataItem AndAlso e.Item.OwnerTableView.Name = "Communications" Then
            Dim parentItem As GridDataItem = e.Item.OwnerTableView.ParentItem
            Dim myDepartmentID As Int32 = parentItem("DepartmentID").Text
            Dim detailTable As GridTableView = DirectCast(e.Item.OwnerTableView, GridTableView)
            If myDepartmentID = 23 Then
                detailTable.GetColumn("EditCommandColumn2").Display = True
                detailTable.GetColumn("DeleteColumn2").Display = True
                detailTable.CommandItemSettings.ShowAddNewRecordButton = True
            Else               

               detailTable.GetColumn("EditCommandColumn2").Display = False
                detailTable.GetColumn("DeleteColumn2").Display = False
                detailTable.CommandItemSettings.ShowAddNewRecordButton = False                       

          End If
        End If

End Sub

 

 

I don't know if I need use Page_Load , DetailTableDataBind  and  PreRender.

Please help.  Thanks. 

0
Perry Blanchard
Top achievements
Rank 1
answered on 05 Jun 2015, 03:14 PM

Jayesh,

I also try RadGridDepartment_DetailTableDataBind, It's not working either.

Private Sub RadGridDepartment_DetailTableDataBind(sender As Object, e As GridDetailTableDataBindEventArgs) Handles RadGridDepartment.DetailTableDataBind

Dim parentItem As GridDataItem = CType(e.DetailTableView.ParentItem, GridDataItem)
If parentItem.Edit Then
Return
End If
If (e.DetailTableView.DataMember = "Communications") Then

If parentItem("DepartmentID").Text = 23 Then
e.DetailTableView.DetailTables(0).GetColumn("EditCommandColumn2").Visible = True
e.DetailTableView.DetailTables(0).GetColumn("DeleteColumn2").Visible = True
e.DetailTableView.CommandItemSettings.ShowAddNewRecordButton = True
Else
e.DetailTableView.DetailTables(0).GetColumn("EditCommandColumn2").Visible = False
e.DetailTableView.DetailTables(0).GetColumn("DeleteColumn2").Visible = False
e.DetailTableView.CommandItemSettings.ShowAddNewRecordButton = False
End If

End If


End Sub

0
JGoswami
Top achievements
Rank 1
answered on 29 May 2020, 09:25 AM
Thanks Jayesh
Tags
General Discussions
Asked by
Riz
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Riz
Top achievements
Rank 1
Kapil
Top achievements
Rank 1
Maria Ilieva
Telerik team
Paulo
Top achievements
Rank 1
Perry Blanchard
Top achievements
Rank 1
JGoswami
Top achievements
Rank 1
Share this question
or