Back to Devexpress

ASPxGridView.GetDetailRowKeyValue(Control) Method

aspnet-devexpress-dot-web-dot-aspxgridview-dot-getdetailrowkeyvalue-x28-system-dot-web-dot-ui-dot-control-x29.md

latest10.7 KB
Original Source

ASPxGridView.GetDetailRowKeyValue(Control) Method

Gets the key value of the grid’s row that is the master row for the specified control used to display detail data.

Namespace : DevExpress.Web

Assembly : DevExpress.Web.v25.2.dll

NuGet Package : DevExpress.Web

Declaration

csharp
public static object GetDetailRowKeyValue(
    Control control
)
vb
Public Shared Function GetDetailRowKeyValue(
    control As Control
) As Object

Parameters

NameTypeDescription
controlControl

A control that represents detail data.

|

Returns

TypeDescription
Object

An object that uniquely identifies the master row containing the specified detail control.

|

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 primary task in these scenarios is to identify a master row when a control that displays the details should be bound to its data.

If a detail control is not ASPxGridView, you can use a master grid’s GetDetailRowKeyValue method to determine in which master row the specified detail control is currently instantiated. This method accepts a detail control instance as a parameter and returns the corresponding master row’s key value.

csharp
protected void GridView1_DataBinding (object sender, EventArgs e) {
        //Standard GridView is used as a detail control.
        GridView detailGrid = sender as GridView;
        detailGrid.DataSource = dsDetailView;
        dsDetailView.SelectParameters["CategoryID"].DefaultValue = ASPxGridView.GetDetailRowKeyValue(detailGrid).ToString();
    }
vb
Protected Sub GridView1_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
        'Standard GridView is used as a detail control.
        Dim detailGrid As GridView = TryCast(sender, GridView)
        detailGrid.DataSource = dsDetailView
        dsDetailView.SelectParameters("CategoryID").DefaultValue = ASPxGridView.GetDetailRowKeyValue(detailGrid.ToString()
    End Sub

Example

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.The simplest way to visualize detail data is to use ASPxGridView, because this control has the GetMasterRowKeyValue() method that automatically obtains the corresponding master row’s key value from a master ASPxGridView. For other cases (when a detail control is not ASPxGridView), you can bring a detail control in correlation with its master row by using a specifically implemented static method - GetDetailRowKeyValue(Control) This method accepts a detail control instance as a parameter and returns the corresponding master row's key value: GetDetailRowKeyValue(Control)

This example demonstrates how to use the GetMasterRowKeyValue() and GetDetailRowKeyValue(Control) methods.

csharp
object keyValue = ASPxGridView.GetDetailRowKeyValue(treeList);
vb
Dim keyValue As Object = ASPxGridView.GetDetailRowKeyValue(treeList)
aspx
<dx:ASPxGridView ID="masterGrid" runat="server" AutoGenerateColumns="False" DataSourceID="dsMainGrid"
    Width="700px" KeyFieldName="CategoryID">
    <Templates>
        <DetailRow>
            <dx:ASPxPageControl ID="ASPxPageControl1" runat="server" ActiveTabIndex="0" Width="100%">
                <TabPages>
                    <dx:TabPage>
                        <ContentCollection>
                            <dx:ContentControl ID="ContentControl1" runat="server">
                                <dx:ASPxTreeList ID="ASPxTreeList1" runat="server" AutoGenerateColumns="False"      
                                                 DataSourceID="dsTreeList" KeyFieldName="EmployeeID" 
                                                 OnInit="ASPxTreeList1_Init" ParentFieldName="ReportsTo"
                                                 Width="100%">
                                    <Columns>
                                        <dx:TreeListTextColumn FieldName="FirstName" />
                                        <dx:TreeListTextColumn FieldName="LastName" />
                                        <dx:TreeListTextColumn FieldName="Title" />
                                    </Columns>
                                </dx:ASPxTreeList>
                            </dx:ContentControl>
                        </ContentCollection>
                    </dx:TabPage>
                    <dx:TabPage>
                        <ContentCollection>
                            <dx:ContentControl ID="ContentControl3" runat="server">
                                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                                              DataKeyNames="ProductID" Style="margin-right: 0px" 
                                              OnDataBinding="GridView1_DataBinding">
                                    <Columns>
                                        <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
                                        <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" SortExpression="UnitPrice" />
                                        <asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock" SortExpression="UnitsInStock" />
                                        <asp:BoundField DataField="UnitsOnOrder" HeaderText="UnitsOnOrder" SortExpression="UnitsOnOrder" />
                                    </Columns>
                                </asp:GridView>
                            </dx:ContentControl>
                        </ContentCollection>
                    </dx:TabPage>
                    <dx:TabPage>
                        <ContentCollection>
                            <dx:ContentControl ID="ContentControl2" runat="server">
                                <dx:ASPxGridView ID="detailGrid" runat="server" AutoGenerateColumns="False" DataSourceID="dsDetail"
                                    KeyFieldName="ProductID" OnBeforePerformDataSelect="detailGrid_BeforePerformDataSelect"
                                    Width="100%">
                                    <Columns>
                                        <dx:GridViewDataTextColumn FieldName="ProductName" VisibleIndex="1">
                                        </dx:GridViewDataTextColumn>
                                        <dx:GridViewDataTextColumn FieldName="SupplierID" VisibleIndex="2">
                                        </dx:GridViewDataTextColumn>
                                        <dx:GridViewDataTextColumn FieldName="CategoryID" VisibleIndex="3">
                                        </dx:GridViewDataTextColumn>
                                        <dx:GridViewDataTextColumn FieldName="QuantityPerUnit" VisibleIndex="4">
                                        </dx:GridViewDataTextColumn>
                                    </Columns>
                                </dx:ASPxGridView>
                            </dx:ContentControl>
                        </ContentCollection>
                    </dx:TabPage>
                </TabPages>
            </dx:ASPxPageControl>
        </DetailRow>
    </Templates>
    <Columns>
        <dx:GridViewDataTextColumn FieldName="CategoryID" ReadOnly="True" >
            <EditFormSettings Visible="False" />
        </dx:GridViewDataTextColumn>
        <dx:GridViewDataTextColumn FieldName="CategoryName" />
        <dx:GridViewDataTextColumn FieldName="Description" />
    </Columns>
    <SettingsDetail ShowDetailRow="True" />
</dx:ASPxGridView>
csharp
protected void ASPxTreeList1_Init (object sender, EventArgs e) {
    ASPxTreeList treeList = sender as ASPxTreeList;
    object keyValue = ASPxGridView.GetDetailRowKeyValue(treeList);
    if (keyValue.Equals(2) || keyValue.Equals(5))
        treeList.RootValue = keyValue;
}

protected void detailGrid_BeforePerformDataSelect (object sender, EventArgs e) {
    ASPxGridView grid = sender as ASPxGridView;
    Session["CategoryID"] = grid.GetMasterRowKeyValue();
}

protected void GridView1_DataBinding (object sender, EventArgs e) {
    GridView gridView = sender as GridView;
    gridView.DataSource = dsDetailView;
    dsDetailView.SelectParameters["CategoryID"].DefaultValue = ASPxGridView.GetDetailRowKeyValue(gridView).ToString();
}
vb
Protected Sub ASPxTreeList1_Init(ByVal sender As Object, ByVal e As EventArgs)
    Dim treeList As ASPxTreeList = TryCast(sender, ASPxTreeList)
    Dim keyValue As Object = ASPxGridView.GetDetailRowKeyValue(treeList)
    If keyValue.Equals(2) OrElse keyValue.Equals(5) Then
        treeList.RootValue = keyValue
    End If
End Sub

Protected Sub detailGrid_BeforePerformDataSelect(ByVal sender As Object, ByVal e As EventArgs)
    Dim grid As ASPxGridView = TryCast(sender, ASPxGridView)
    Session("CategoryID") = grid.GetMasterRowKeyValue()
End Sub

Protected Sub GridView1_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
    Dim gridView As GridView = TryCast(sender, GridView)
    gridView.DataSource = dsDetailView
    dsDetailView.SelectParameters("CategoryID").DefaultValue = ASPxGridView.GetDetailRowKeyValue(gridView).ToString()
End Sub

See Also

GetMasterRowKeyValue()

GetDetailRowValues(Control, String[])

Grid View

ASPxGridView Class

ASPxGridView Members

DevExpress.Web Namespace