ASPxGridView.GetMasterRowKeyValue() Method
Returns the master row’s key value.
Namespace: DevExpress.Web
Assembly: DevExpress.Web.v24.1.dll
NuGet Package: DevExpress.Web
Declaration
Returns
Type | Description |
---|---|
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.
protected void detailGrid_BeforePerformDataSelect(object sender, EventArgs e) {
Session["CategoryID"] = (sender as ASPxGridView).GetMasterRowKeyValue();
}
Example
This example demonstrates how to provide detail data on-the-fly. This is done by handling the detail grid’s DataBinding event.
<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>
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;
}
}
Related GitHub Examples
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.