11 Answers, 1 is accepted
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
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
Can you please provide your grid mark up (aspx page code)?
Thanks,
Jayesh Goyani
<%@ 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();
}
}
}
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
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.
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