Back to Devexpress

ASPxGridView.GetMasterRowKeyValue() Method

aspnet-devexpress-dot-web-dot-aspxgridview-028be2fc.md

latest11.7 KB
Original Source

ASPxGridView.GetMasterRowKeyValue() Method

Returns the master row’s key value.

Namespace : DevExpress.Web

Assembly : DevExpress.Web.v25.2.dll

NuGet Package : DevExpress.Web

Declaration

csharp
public object GetMasterRowKeyValue()
vb
Public Function GetMasterRowKeyValue As Object

Returns

TypeDescription
Object

An object that uniquely identifies the master row.

|

Remarks

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.

csharp
protected void detailGrid_BeforePerformDataSelect(object sender, EventArgs e) {
    Session["CategoryID"] = (sender as ASPxGridView).GetMasterRowKeyValue();
}
vb
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.

aspx
<dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" Width="511px">
    <Columns>
        <dx:GridViewDataTextColumn FieldName="ID" />
        <dx:GridViewDataTextColumn FieldName="ParentID" />
        <dx:GridViewDataTextColumn FieldName="Name" />
    </Columns>
    <SettingsDetail ShowDetailRow="True" />
    <Templates>
        <DetailRow>
            <dx:ASPxGridView ID="ASPxGridView2" runat="server" AutoGenerateColumns="False"
                KeyFieldName="ID" OnDataBinding="ASPxGridView2_DataBinding">
                <Columns>
                    <dx:GridViewDataTextColumn FieldName="ID" />
                    <dx:GridViewDataTextColumn FieldName="ParentID" />
                    <dx:GridViewDataTextColumn FieldName="Name" />
                </Columns>
                <SettingsDetail IsDetailGrid="True" />
            </dx:ASPxGridView>
        </DetailRow>
    </Templates>
</dx:ASPxGridView>
csharp
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;
    }
}
vb
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

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.

aspxgridview-edit-in-memory-dataset/CS/WebApp/Default.aspx.cs#L42

csharp
ASPxGridView detailGridView = (ASPxGridView)sender;
dv.RowFilter = "MasterID = " + detailGridView.GetMasterRowKeyValue();
detailGridView.DataSource = dv;

asp-net-web-forms-grid-create-master-detail-grid-at-runtime/CS/MasterDetailGrids/Default.aspx.cs#L113

csharp
cmd.Parameters.Add("@SupplierID", SqlDbType.Int).Value = randomizer.Next(1, 29);
cmd.Parameters.Add("@CategoryID", SqlDbType.Int).Value = senderGridView.GetMasterRowKeyValue();
cmd.Parameters.Add("@UnitPrice", SqlDbType.Decimal).Value = e.NewValues["UnitPrice"];

asp-net-web-forms-grid-entitydatasource-and-common-scenarios/CS/GridEntityFramework/MasterDetail.aspx.cs#L35

csharp
ASPxGridView grid = sender as ASPxGridView;
int masterKey = (int)grid.GetMasterRowKeyValue();
grid.DataSource = DataContext.Products.Where(p => p.CategoryID == masterKey).ToList();

asp-net-web-forms-grid-bind-master-and-detail-grids-to-list-data-sources/CS/Default.aspx.cs#L47

csharp
ASPxGridView detailGrid = (ASPxGridView)sender;
int id = (int)detailGrid.GetMasterRowKeyValue();

aspxgridview-edit-in-memory-dataset/VB/WebApp/Default.aspx.vb#L49

vb
Dim detailGridView As ASPxGridView = CType(sender, ASPxGridView)
dv.RowFilter = "MasterID = " & detailGridView.GetMasterRowKeyValue()
detailGridView.DataSource = dv

asp-net-web-forms-grid-create-master-detail-grid-at-runtime/VB/MasterDetailGrids/Default.aspx.vb#L117

vb
cmd.Parameters.Add("@SupplierID", SqlDbType.Int).Value = randomizer.Next(1, 29)
cmd.Parameters.Add("@CategoryID", SqlDbType.Int).Value = senderGridView.GetMasterRowKeyValue()
cmd.Parameters.Add("@UnitPrice", SqlDbType.Decimal).Value = e.NewValues("UnitPrice")

asp-net-web-forms-grid-entitydatasource-and-common-scenarios/VB/GridEntityFramework/MasterDetail.aspx.vb#L37

vb
Dim grid As ASPxGridView = TryCast(sender, ASPxGridView)
Dim masterKey As Integer = CInt(grid.GetMasterRowKeyValue())
grid.DataSource = DataContext.Products.Where(Function(p) p.CategoryID = masterKey).ToList()

asp-net-web-forms-grid-bind-master-and-detail-grids-to-list-data-sources/VB/Default.aspx.vb#L45

vb
Dim detailGrid As ASPxGridView = CType(sender, ASPxGridView)
Dim id As Integer = CInt(detailGrid.GetMasterRowKeyValue())
Dim result = From q In ChildData Where q.ParentID = id Select q

See Also

GetMasterRowFieldValues(String[])

GetDetailRowKeyValue(Control)

Grid View

ASPxGridView Class

ASPxGridView Members

DevExpress.Web Namespace