我怎样才能使 RadGrid 根据单击的命令只展开一个细节 table?
How can I make RadGrid expand only one detail table based on the command clicked?
我有一个主视图和两个子视图。我想在主行上有命令按钮,这会导致一个或其他子表发生扩展。当我处理 ItemCommand 事件时,我隐藏了将所有详细信息表设置为 Visible=False 但它们在输出中仍然可见。
我试过在 ItemCommand 事件中设置详细信息表的展开和可见性,如下所示:
if (e.CommandName == "ExpandSomething")
{
item.Expanded = true;
foreach (var table in MyGrid.MasterTableView.DetailTables)
if (table.Name != "Something")
table.Visible = false;
}
这对网格没有影响。
然后我想也许隐藏需要在事件周期的后期完成,所以我试了一下:
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;
}
两种方法都行不通 - 两个网格始终可见。
在尝试不同的事件时,我发现 DataBinding 事件是唯一将可见性设置为错误的地方,但不幸的是它在 ItemCommand 之前触发所以我无法确定是否有详细信息按钮被按下了。
对于像所描述的那样的条件详细视图还有其他想法吗?
编辑,这是标记,如果您认为它会有所帮助的话。我删除了不需要的列定义:
<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>
请尝试使用以下代码片段。
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>
方法一:每行只显示1个子网格。
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;
}
}
}
方法二:全格只显示1个子格
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;
}
}
}
}
如有任何疑问,请告诉我。
我有一个主视图和两个子视图。我想在主行上有命令按钮,这会导致一个或其他子表发生扩展。当我处理 ItemCommand 事件时,我隐藏了将所有详细信息表设置为 Visible=False 但它们在输出中仍然可见。
我试过在 ItemCommand 事件中设置详细信息表的展开和可见性,如下所示:
if (e.CommandName == "ExpandSomething")
{
item.Expanded = true;
foreach (var table in MyGrid.MasterTableView.DetailTables)
if (table.Name != "Something")
table.Visible = false;
}
这对网格没有影响。
然后我想也许隐藏需要在事件周期的后期完成,所以我试了一下:
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;
}
两种方法都行不通 - 两个网格始终可见。
在尝试不同的事件时,我发现 DataBinding 事件是唯一将可见性设置为错误的地方,但不幸的是它在 ItemCommand 之前触发所以我无法确定是否有详细信息按钮被按下了。
对于像所描述的那样的条件详细视图还有其他想法吗?
编辑,这是标记,如果您认为它会有所帮助的话。我删除了不需要的列定义:
<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>
请尝试使用以下代码片段。
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>
方法一:每行只显示1个子网格。
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;
}
}
}
方法二:全格只显示1个子格
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;
}
}
}
}
如有任何疑问,请告诉我。