I have a master view with two child views. I would like to have command buttons on the master row which cause an expand to happen to one or the other child tables. When I handle the ItemCommand event, I hide set all detail tables to Visible=False but they are still visible on the output.
I've tried setting the expanded and visibility of the detail tables right within the ItemCommand event like this:
if (e.CommandName == "ExpandSomething")
{
item.Expanded = true;
foreach (var table in MyGrid.MasterTableView.DetailTables)
if (table.Name != "Something")
table.Visible = false;
}
This had no effect on the Grid.
Then I thought maybe the hiding needed to be done later in the event cycle so I tried it like this:
private string _activeChildView = null;
protected void ShowSingleChildTable(string name) //this is called from item command
{
_activeChildView = name;
}
private void Page_PreRender(object sender, EventArgs e)
{
if (_activeChildView != null)
foreach (var table in AdminGrid.MasterTableView.DetailTables)
if (table.Name != _activeChildView)
table.Visible = false;
}
Neither way works - both grids are always visible.
In playing around with different events, I've found that the DataBinding event is the only place that setting the visibility to false works, but unfortunately that fires before the ItemCommand so I can't figure out if a detail button was pressed.
Any other ideas for a conditional detail view like the one described?
EDIT, here's the markup, if you think it'll help. I've removed the unneeded column definitions:
<asp:EntityDataSource ID="EntityDataSourceThinges" runat="server"
ConnectionString="name=MyProjectEntities"
EntitySetName="Thinges" DefaultContainerName="MyProjectEntities"
EnableDelete="true" EnableUpdate="true" EnableInsert="true"
Include="ThingSomethings"
OrderBy="it.Title"
Where="it.Title like '%' + @Title + '%' or @Title is null">
<WhereParameters>
<asp:ControlParameter ControlID="txtSearch" DefaultValue="%" Name="Title" PropertyName="Text" Type="String" />
</WhereParameters>
</asp:EntityDataSource>
<asp:EntityDataSource ID="EntityDataSourceThingSomethings" runat="server"
ConnectionString="name=MyProjectEntities"
EntitySetName="ThingSomethings" DefaultContainerName="MyProjectEntities"
EnableDelete="true" EnableUpdate="true"
AutoGenerateWhereClause="true">
<WhereParameters>
<asp:SessionParameter Name="ThingID" SessionField="ThingID" Type="Int32" />
</WhereParameters>
</asp:EntityDataSource>
<asp:EntityDataSource ID="EntityDataSourceImages" runat="server"
ConnectionString="name=MyProjectEntities"
EntitySetName="MyProjectImages" DefaultContainerName="MyProjectEntities"
EnableDelete="true" EnableUpdate="true" EnableInsert="true"
AutoGenerateWhereClause="true"
OrderBy="it.Sequence">
<WhereParameters>
<asp:SessionParameter Name="ThingID" SessionField="ThingID" Type="Int32" />
</WhereParameters>
</asp:EntityDataSource>
Search by title:
<asp:TextBox runat="server" ID="txtSearch" /><asp:Button runat="server" Text="Search" />
<telerik:RadGrid ID="DnnGrid1" runat="server"
DataSourceID="EntityDataSourceThinges"
ImagesPath="~/images"
AllowPaging="false" AllowSorting="true"
AllowAutomaticUpdates="true" AllowAutomaticInserts="true" AllowAutomaticDeletes="true">
<MasterTableView AutoGenerateColumns="false" CommandItemDisplay="TopAndBottom"
HierarchyLoadMode="ServerOnDemand" ExpandCollapseColumn-Visible="false"
DataKeyNames="ThingID">
<Columns>
...
<telerik:GridButtonColumn Text="Expand Something" CommandName="ExpandSomething" />
<telerik:GridButtonColumn Text="Expand Something Else" CommandName="ExpandSomethingElse" />
...
<telerik:GridButtonColumn ButtonType="ImageButton" CommandName="Delete" ConfirmText="Are you sure you want to permenently delete this record?" />
</Columns>
<DetailTables>
<telerik:GridTableView runat="server" Name="Images" DataKeyNames="MyProjectImageID" DataSourceID="EntityDataSourceImages"
AutoGenerateColumns="false"
AllowAutomaticUpdates="true" AllowAutomaticInserts="true" AllowAutomaticDeletes="true"
CommandItemDisplay="TopAndBottom">
<ParentTableRelation>
<telerik:GridRelationFields MasterKeyField="ThingID" DetailKeyField="ThingID" />
</ParentTableRelation>
<Columns>
...
</Columns>
</telerik:GridTableView>
<telerik:GridTableView runat="server" Name="Something" DataSourceID="EntityDataSourceThingSomethings"
AutoGenerateColumns="false"
AllowAutomaticUpdates="true" AllowAutomaticDeletes="true"
CommandItemDisplay="TopAndBottom" DataKeyNames="SomethingID,ThingID">
<ParentTableRelation>
<telerik:GridRelationFields MasterKeyField="ThingID" DetailKeyField="ThingID" />
</ParentTableRelation>
<Columns>
...
</Columns>
<EditFormSettings EditFormType="AutoGenerated" />
<CommandItemSettings ShowAddNewRecordButton="false" />
</telerik:GridTableView>
</DetailTables>
<EditFormSettings EditFormType="AutoGenerated" />
</MasterTableView>
</telerik:RadGrid>
Please try with the below code snippet.
ASPX
<telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource" OnDetailTableDataBind="RadGrid1_DetailTableDataBind"
OnItemCommand="RadGrid1_ItemCommand" AutoGenerateColumns="false">
<MasterTableView DataKeyNames="ID">
<Columns>
<telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
</telerik:GridBoundColumn>
<telerik:GridButtonColumn Text="Expand Something" CommandName="ExpandSomething" />
<telerik:GridButtonColumn Text="Expand Something Else" CommandName="ExpandSomethingElse" />
</Columns>
<DetailTables>
<telerik:GridTableView runat="server" Name="Images" AutoGenerateColumns="false" CommandItemDisplay="TopAndBottom">
<Columns>
<telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
</telerik:GridBoundColumn>
</Columns>
</telerik:GridTableView>
<telerik:GridTableView runat="server" Name="Something" AutoGenerateColumns="false" CommandItemDisplay="TopAndBottom">
<Columns>
<telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
</telerik:GridBoundColumn>
</Columns>
</telerik:GridTableView>
</DetailTables>
</MasterTableView>
</telerik:RadGrid>
Method 1: Show only 1 child grid in each row.
ASPX.CS
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
dynamic data = new[] {
new { ID = 1, Name ="Name1"},
new { ID = 2, Name ="Name2"} };
RadGrid1.DataSource = data;
}
protected void RadGrid1_DetailTableDataBind(object sender, GridDetailTableDataBindEventArgs e)
{
GridDataItem parentItem = e.DetailTableView.ParentItem as GridDataItem;
if (e.DetailTableView.Name == "Images")
{
dynamic data = new[] {
new { ID = 11, Name ="Name11"},
new { ID = 12, Name ="Name12"} };
e.DetailTableView.DataSource = data;
}
else if (e.DetailTableView.Name == "Something")
{
dynamic data = new[] {
new { ID = 111, Name ="Name111"},
new { ID = 112, Name ="Name112"} };
e.DetailTableView.DataSource = data;
}
}
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
if (e.CommandName == "ExpandSomething")
{
GridDataItem item = e.Item as GridDataItem;
item.Expanded = true;
if (e.Item.HasChildItems)
{
item.ChildItem.NestedTableViews[0].Visible = false;
item.ChildItem.NestedTableViews[1].Visible = true;
}
}
else if (e.CommandName == "ExpandSomethingElse")
{
GridDataItem item = e.Item as GridDataItem;
item.Expanded = true;
if (e.Item.HasChildItems)
{
item.ChildItem.NestedTableViews[0].Visible = true;
item.ChildItem.NestedTableViews[1].Visible = false;
}
}
}
Method 2: Show only 1 child grid in full grid.
ASPX.CS
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
dynamic data = new[] {
new { ID = 1, Name ="Name1"},
new { ID = 2, Name ="Name2"} };
RadGrid1.DataSource = data;
}
protected void RadGrid1_DetailTableDataBind(object sender, GridDetailTableDataBindEventArgs e)
{
GridDataItem parentItem = e.DetailTableView.ParentItem as GridDataItem;
if (e.DetailTableView.Name == "Images")
{
dynamic data = new[] {
new { ID = 11, Name ="Name11"},
new { ID = 12, Name ="Name12"} };
e.DetailTableView.DataSource = data;
}
else if (e.DetailTableView.Name == "Something")
{
dynamic data = new[] {
new { ID = 111, Name ="Name111"},
new { ID = 112, Name ="Name112"} };
e.DetailTableView.DataSource = data;
}
}
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
if (e.CommandName == "ExpandSomething")
{
GridDataItem item = e.Item as GridDataItem;
item.Expanded = true;
if (e.Item.HasChildItems)
{
item.ChildItem.NestedTableViews[0].Visible = false;
item.ChildItem.NestedTableViews[1].Visible = true;
}
foreach (GridDataItem otheritem in RadGrid1.MasterTableView.Items)
{
if (otheritem.Expanded && item.GetDataKeyValue("ID").ToString() != otheritem.GetDataKeyValue("ID").ToString())
{
otheritem.Expanded = false;
}
}
}
else if (e.CommandName == "ExpandSomethingElse")
{
GridDataItem item = e.Item as GridDataItem;
item.Expanded = true;
if (e.Item.HasChildItems)
{
item.ChildItem.NestedTableViews[0].Visible = true;
item.ChildItem.NestedTableViews[1].Visible = false;
}
foreach (GridDataItem otheritem in RadGrid1.MasterTableView.Items)
{
if (otheritem.Expanded && item.GetDataKeyValue("ID").ToString() != otheritem.GetDataKeyValue("ID").ToString())
{
otheritem.Expanded = false;
}
}
}
}
Let me know if any concern.