GridViewBaseRowTemplateContainer.KeyValue Property
Gets an object that uniquely identifies the row that contains the template container.
Namespace: DevExpress.Web
Assembly:
DevExpress.Web.v26.1.dll
NuGet Package:
DevExpress.Web
Declaration
public virtual object KeyValue { get; }
Public Overridable ReadOnly Property KeyValue As Object
Property Value
| Type |
Description |
| Object |
An object that uniquely identifies the row that contains the template container.
|
Example
Sometimes it is necessary to show hierarchical data inside a detail ASPxGridView row. ASPxTreeList can show hierarchical data, but it does not have the built-in capability to get a corresponding master row key value.
To get a grid master row key value, it is necessary to move up through a hierarchy of controls to find the GridViewBaseRowTemplateContainer that has the KeyValue property. This is a universal solution and it can be applied to any ASP.NET control held in a grid template.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxTreeList;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e) {
}
protected object GetMasterRowKeyValue(ASPxTreeList treeList) {
GridViewBaseRowTemplateContainer container = null;
Control control = treeList;
while(control.Parent != null) {
container = control.Parent as GridViewBaseRowTemplateContainer;
if(container != null) break;
control = control.Parent;
}
return container.KeyValue;
}
protected void ASPxTreeList1_Init(object sender, EventArgs e) {
ASPxTreeList treeList = sender as ASPxTreeList;
object keyValue = GetMasterRowKeyValue(treeList);
treeList.RootValue = keyValue;
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="DevExpress.Web.ASPxGridView.v11.1, Version=11.1.7.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web.ASPxGridView" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.ASPxEditors.v11.1, Version=11.1.7.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web.ASPxEditors" TagPrefix="dxe" %>
<%@ Register Assembly="DevExpress.Web.ASPxTreeList.v11.1, Version=11.1.7.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web.ASPxTreeList" TagPrefix="dx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" DataSourceID="AccessDataSource1"
KeyFieldName="ID">
<Templates>
<DetailRow>
<dx:ASPxTreeList ID="ASPxTreeList1" runat="server" AutoGenerateColumns="False"
DataSourceID="AccessDataSource1" KeyFieldName="ID" OnInit="ASPxTreeList1_Init"
ParentFieldName="PARENTID">
<Columns>
<dx:TreeListTextColumn FieldName="DEPARTMENT" VisibleIndex="0">
</dx:TreeListTextColumn>
</Columns>
</dx:ASPxTreeList>
</DetailRow>
</Templates>
<Columns>
<dx:GridViewDataTextColumn FieldName="ID" ReadOnly="True" VisibleIndex="0">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="PARENTID" VisibleIndex="1">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="DEPARTMENT" VisibleIndex="2">
</dx:GridViewDataTextColumn>
</Columns>
<SettingsDetail ShowDetailRow="True" />
</dx:ASPxGridView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/Departments.mdb"
DeleteCommand="DELETE FROM [Departments] WHERE (([ID] = ?) OR ([ID] IS NULL AND ? IS NULL))"
InsertCommand="INSERT INTO [Departments] ([ID], [PARENTID], [DEPARTMENT]) VALUES (?, ?, ?)"
SelectCommand="SELECT [ID], [PARENTID], [DEPARTMENT] FROM [Departments]" UpdateCommand="UPDATE [Departments] SET [PARENTID] = ?, [DEPARTMENT] = ? WHERE (([ID] = ?) OR ([ID] IS NULL AND ? IS NULL))">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Double" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="PARENTID" Type="Double" />
<asp:Parameter Name="DEPARTMENT" Type="String" />
<asp:Parameter Name="ID" Type="Double" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="ID" Type="Double" />
<asp:Parameter Name="PARENTID" Type="Double" />
<asp:Parameter Name="DEPARTMENT" Type="String" />
</InsertParameters>
</asp:AccessDataSource>
</div>
</form>
</body>
</html>
<%@ Page Language="vb" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<%@ Register Assembly="DevExpress.Web.ASPxGridView.v11.1, Version=11.1.7.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web.ASPxGridView" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.ASPxEditors.v11.1, Version=11.1.7.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web.ASPxEditors" TagPrefix="dxe" %>
<%@ Register Assembly="DevExpress.Web.ASPxTreeList.v11.1, Version=11.1.7.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web.ASPxTreeList" TagPrefix="dx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" DataSourceID="AccessDataSource1"
KeyFieldName="ID">
<Templates>
<DetailRow>
<dx:ASPxTreeList ID="ASPxTreeList1" runat="server" AutoGenerateColumns="False"
DataSourceID="AccessDataSource1" KeyFieldName="ID" OnInit="ASPxTreeList1_Init"
ParentFieldName="PARENTID">
<Columns>
<dx:TreeListTextColumn FieldName="DEPARTMENT" VisibleIndex="0">
</dx:TreeListTextColumn>
</Columns>
</dx:ASPxTreeList>
</DetailRow>
</Templates>
<Columns>
<dx:GridViewDataTextColumn FieldName="ID" ReadOnly="True" VisibleIndex="0">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="PARENTID" VisibleIndex="1">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="DEPARTMENT" VisibleIndex="2">
</dx:GridViewDataTextColumn>
</Columns>
<SettingsDetail ShowDetailRow="True" />
</dx:ASPxGridView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/Departments.mdb"
DeleteCommand="DELETE FROM [Departments] WHERE (([ID] = ?) OR ([ID] IS NULL AND ? IS NULL))"
InsertCommand="INSERT INTO [Departments] ([ID], [PARENTID], [DEPARTMENT]) VALUES (?, ?, ?)"
SelectCommand="SELECT [ID], [PARENTID], [DEPARTMENT] FROM [Departments]" UpdateCommand="UPDATE [Departments] SET [PARENTID] = ?, [DEPARTMENT] = ? WHERE (([ID] = ?) OR ([ID] IS NULL AND ? IS NULL))">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Double" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="PARENTID" Type="Double" />
<asp:Parameter Name="DEPARTMENT" Type="String" />
<asp:Parameter Name="ID" Type="Double" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="ID" Type="Double" />
<asp:Parameter Name="PARENTID" Type="Double" />
<asp:Parameter Name="DEPARTMENT" Type="String" />
</InsertParameters>
</asp:AccessDataSource>
</div>
</form>
</body>
</html>
Imports Microsoft.VisualBasic
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports DevExpress.Web.ASPxGridView
Imports DevExpress.Web.ASPxTreeList
Partial Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Protected Function GetMasterRowKeyValue(ByVal treeList As ASPxTreeList) As Object
Dim container As GridViewBaseRowTemplateContainer = Nothing
Dim control As Control = treeList
Do While control.Parent IsNot Nothing
container = TryCast(control.Parent, GridViewBaseRowTemplateContainer)
If container IsNot Nothing Then
Exit Do
End If
control = control.Parent
Loop
Return container.KeyValue
End Function
Protected Sub ASPxTreeList1_Init(ByVal sender As Object, ByVal e As EventArgs)
Dim treeList As ASPxTreeList = TryCast(sender, ASPxTreeList)
Dim keyValue As Object = GetMasterRowKeyValue(treeList)
treeList.RootValue = keyValue
End Sub
End Class
See Also