ASPxGridView.GetMasterRowKeyValue() Method
Returns the master row’s key value.
Namespace: DevExpress.Web
Assembly:
DevExpress.Web.v20.2.dll
NuGet Package:
DevExpress.Web
Declaration
public object GetMasterRowKeyValue()
Public Function GetMasterRowKeyValue As Object
Returns
Type |
Description |
Object |
An object that uniquely identifies the master row.
|
In master-detail scenarios, when ASPxGridView is used as a master grid that displays details within its detail rows, you can visualize detail data using any desired control. The main task in these scenarios is to identify a master row when a control that displays the details should be bound to its data.
If the ASPxGridView is used as a detail grid, its GetMasterRowKeyValue method is able to automatically identify the master row in which the detail grid’s instance is displayed. Use the GetMasterRowKeyValue method of a detail ASPxGridView within a handler of the detail grid’s data binding event (ASPxGridBase.BeforePerformDataSelect or DataBinding) to obtain the corresponding master row’s key value from a master ASPxGridView.
protected void detailGrid_BeforePerformDataSelect(object sender, EventArgs e) {
Session["CategoryID"] = (sender as ASPxGridView).GetMasterRowKeyValue();
}
Protected Sub detailGrid_BeforePerformDataSelect(ByVal sender As Object,_
ByVal e As System.EventArgs) Handles ASPxGridView1.BeforePerformDataSelect
Session("CategoryID") = CType(sender, ASPxGridView).GetMasterRowKeyValue()
End Sub
Example
This example demonstrates how to provide detail data on-the-fly. This is done by handling the detail grid's DataBinding event.See also:
How to use different components to display details of a master-detail ASPxGridViewHow to use different components to display details of a master-detail ASPxGridViewE3604: How to use different components to display details of a master-detail ASPxGridView
View Example
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="DevExpress.Web.ASPxGridView.v8.1" Namespace="DevExpress.Web.ASPxGridView" TagPrefix="dxwgv" %>
<%@ Register Assembly="DevExpress.Web.ASPxEditors.v8.1" Namespace="DevExpress.Web.ASPxEditors" TagPrefix="dxe" %>
<!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>Load detail data dynamically (self-referenced dataset example)</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<dxwgv:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False"
Width="511px">
<Columns>
<dxwgv:GridViewDataTextColumn FieldName="ID" VisibleIndex="0">
</dxwgv:GridViewDataTextColumn>
<dxwgv:GridViewDataTextColumn FieldName="ParentID" VisibleIndex="1">
</dxwgv:GridViewDataTextColumn>
<dxwgv:GridViewDataTextColumn FieldName="Name" VisibleIndex="2">
</dxwgv:GridViewDataTextColumn>
</Columns>
<SettingsDetail ShowDetailRow="True" />
<Templates>
<DetailRow>
<dxwgv:ASPxGridView ID="ASPxGridView2" runat="server" AutoGenerateColumns="False"
Width="511px" KeyFieldName="ID" OnDataBinding="ASPxGridView2_DataBinding">
<Columns>
<dxwgv:GridViewDataTextColumn FieldName="ID" VisibleIndex="0">
</dxwgv:GridViewDataTextColumn>
<dxwgv:GridViewDataTextColumn FieldName="ParentID" VisibleIndex="1">
</dxwgv:GridViewDataTextColumn>
<dxwgv:GridViewDataTextColumn FieldName="Name" VisibleIndex="2">
</dxwgv:GridViewDataTextColumn>
</Columns>
<SettingsDetail IsDetailGrid="True" />
</dxwgv:ASPxGridView>
</DetailRow>
</Templates>
</dxwgv:ASPxGridView>
</div>
</form>
</body>
</html>
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;
public partial class _Default : System.Web.UI.Page {
DataSet ds;
DataTable table;
protected void Page_Load(object sender, EventArgs e) {
ds = new DataSet();
table = ds.Tables.Add("People");
table.Columns.Add("ID", typeof(int));
table.Columns.Add("ParentID", typeof(int));
table.Columns.Add("Name", typeof(string));
table.PrimaryKey = new DataColumn[] { table.Columns["ID"] };
table.Rows.Add(new object[] { 1, DBNull.Value, "Bill" }) ;
table.Rows.Add(new object[] { 2, DBNull.Value, "John" });
table.Rows.Add(new object[] { 3, DBNull.Value, "Clive" });
table.Rows.Add(new object[] { 4, 1, "Ann" });
table.Rows.Add(new object[] { 5, 1, "Tom" });
table.Rows.Add(new object[] { 6, 3, "Jane" });
ds.Relations.Add("ParentChildren", table.Columns["ID"], table.Columns["ParentID"]);
DataView dv = new DataView(table);
dv.RowFilter = "[ParentID] Is Null";
ASPxGridView1.KeyFieldName = "ID";
ASPxGridView1.DataSource = dv;
ASPxGridView1.DataBind();
}
protected void ASPxGridView2_DataBinding(object sender, EventArgs e) {
ASPxGridView detailGrid = (ASPxGridView)sender;
object id = detailGrid.GetMasterRowKeyValue();
DataView detailData = new DataView(table);
detailData.RowFilter = String.Format("[ParentID] = {0}", id);
detailGrid.DataSource = detailData;
}
}
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
Partial Public Class _Default
Inherits System.Web.UI.Page
Private ds As DataSet
Private table As DataTable
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
ds = New DataSet()
table = ds.Tables.Add("People")
table.Columns.Add("ID", GetType(Integer))
table.Columns.Add("ParentID", GetType(Integer))
table.Columns.Add("Name", GetType(String))
table.PrimaryKey = New DataColumn() { table.Columns("ID") }
table.Rows.Add(New Object() { 1, DBNull.Value, "Bill" })
table.Rows.Add(New Object() { 2, DBNull.Value, "John" })
table.Rows.Add(New Object() { 3, DBNull.Value, "Clive" })
table.Rows.Add(New Object() { 4, 1, "Ann" })
table.Rows.Add(New Object() { 5, 1, "Tom" })
table.Rows.Add(New Object() { 6, 3, "Jane" })
ds.Relations.Add("ParentChildren", table.Columns("ID"), table.Columns("ParentID"))
Dim dv As DataView = New DataView(table)
dv.RowFilter = "[ParentID] Is Null"
ASPxGridView1.KeyFieldName = "ID"
ASPxGridView1.DataSource = dv
ASPxGridView1.DataBind()
End Sub
Protected Sub ASPxGridView2_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
Dim detailGrid As ASPxGridView = CType(sender, ASPxGridView)
Dim id As Object = detailGrid.GetMasterRowKeyValue()
Dim detailData As DataView = New DataView(table)
detailData.RowFilter = String.Format("[ParentID] = {0}", id)
detailGrid.DataSource = detailData
End Sub
End Class
<%@ Page Language="vb" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<%@ Register Assembly="DevExpress.Web.ASPxGridView.v7.3" Namespace="DevExpress.Web.ASPxGridView" TagPrefix="dxwgv" %>
<%@ Register Assembly="DevExpress.Web.ASPxEditors.v7.3" Namespace="DevExpress.Web.ASPxEditors" TagPrefix="dxe" %>
<!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>Load detail data dynamically (self-referenced dataset example)</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<dxwgv:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False"
Width="511px">
<Columns>
<dxwgv:GridViewDataTextColumn FieldName="ID" VisibleIndex="0">
</dxwgv:GridViewDataTextColumn>
<dxwgv:GridViewDataTextColumn FieldName="ParentID" VisibleIndex="1">
</dxwgv:GridViewDataTextColumn>
<dxwgv:GridViewDataTextColumn FieldName="Name" VisibleIndex="2">
</dxwgv:GridViewDataTextColumn>
</Columns>
<SettingsDetail ShowDetailRow="True" />
<Templates>
<DetailRow>
<dxwgv:ASPxGridView ID="ASPxGridView2" runat="server" AutoGenerateColumns="False"
Width="511px" KeyFieldName="ID" OnDataBinding="ASPxGridView2_DataBinding">
<Columns>
<dxwgv:GridViewDataTextColumn FieldName="ID" VisibleIndex="0">
</dxwgv:GridViewDataTextColumn>
<dxwgv:GridViewDataTextColumn FieldName="ParentID" VisibleIndex="1">
</dxwgv:GridViewDataTextColumn>
<dxwgv:GridViewDataTextColumn FieldName="Name" VisibleIndex="2">
</dxwgv:GridViewDataTextColumn>
</Columns>
<SettingsDetail IsDetailGrid="True" />
</dxwgv:ASPxGridView>
</DetailRow>
</Templates>
</dxwgv:ASPxGridView>
</div>
</form>
</body>
</html>
The following code snippets (auto-collected from DevExpress Examples) contain references to the GetMasterRowKeyValue() method.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.
See Also