Back to Devexpress

ASPxGridView.MakeRowVisible(Object) Method

aspnet-devexpress-dot-web-dot-aspxgridview-dot-makerowvisible-x28-system-dot-object-x29.md

latest31.8 KB
Original Source

ASPxGridView.MakeRowVisible(Object) Method

Makes the specified data row visible on screen.

Namespace : DevExpress.Web

Assembly : DevExpress.Web.v25.2.dll

NuGet Package : DevExpress.Web

Declaration

csharp
public bool MakeRowVisible(
    object keyValue
)
vb
Public Function MakeRowVisible(
    keyValue As Object
) As Boolean

Parameters

NameTypeDescription
keyValueObject

An object that identifies the data row by its key value.

|

Returns

TypeDescription
Boolean

true, if the specified data row has been made visible on screen; otherwise, false.

|

Remarks

If page-mode navigation is enabled, use the MakeRowVisible method to switch to the page which displays the specified data row (if required). If the row is not found, the MakeRowVisible method does nothing and returns false.

Note

The MakeRowVisible method is not in effect if the endless paging mode is enabled.

Example

This example demonstrates how you can respond to insertion of a new master row into the grid to allow an end-user to enter the corresponding details immediately.In this sample, the ASPxGridView is used in master-detail mode. Grid detail rows are represented by an ASPxPageControl, which is placed within the grid's DetailRow template.

The ASPxPageControl has two tab pages, which contain different UserControls.

The first tab page has an ASPxMemo editor displaying master row description info. The second tab page's content is represented by another ASPxGridView control that maintains master row details.

By default, the first tab page is active.After an end-user inserts a new master row into the master grid, it is required to focus this row, open its detail row, make the ASPxPageControl's second tab page active and switch the detail grid to the insert mode.This behavior is implemented by handling the master ASPxGridView's RowInserting event and the ASPxPageControl's DataBound event.

csharp
using System;
using DevExpress.Web;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e) {

    }

    protected void GridView_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e){
        ASPxGridView gridView = (ASPxGridView)sender;

        GridDataSource gridDataSource = new GridDataSource();
        gridDataSource.InsertRow(e.NewValues, gridView.SettingsDetail.IsDetailGrid);

        gridView.CancelEdit();
        e.Cancel = true;

        //Navigate to the newly inserted row within the grid and open its details
        gridView.MakeRowVisible(e.NewValues["ID"]);
        gridView.DetailRows.ExpandRowByKey(e.NewValues["ID"]);

        activeTabIndex = 1;
    }

    private int? activeTabIndex;

    protected void ASPxPageControl1_DataBound(object sender, EventArgs e){
        if (activeTabIndex.HasValue){
            //Change the ASPxPageControl's active tab page, and switch the detail grid to insert mode
            ASPxPageControl pageControl = sender as ASPxPageControl;
            pageControl.ActiveTabIndex = activeTabIndex.Value;
            ASPxGridView detailGrid = pageControl.FindControl("WebUserControl2").FindControl("gvDetail") as ASPxGridView;
            detailGrid.AddNewRow();
        }
    }
}
aspx
<%-- BeginRegion Page starting tags --%>

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register Assembly="DevExpress.Web.v15.1, Version=15.1.2.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
    Namespace="DevExpress.Web" TagPrefix="dxwgv" %>
<%@ Register Assembly="DevExpress.Web.v15.1, Version=15.1.2.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
    Namespace="DevExpress.Web" TagPrefix="dxe" %>

<%@ Register Assembly="DevExpress.Web.v15.1, Version=15.1.2.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
    Namespace="DevExpress.Web" TagPrefix="dxw" %>
<%@ Register Assembly="DevExpress.Web.v15.1, Version=15.1.2.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
    Namespace="DevExpress.Web" TagPrefix="dxtc" %>

<%@ Register Src="WebUserControl1.ascx" TagName="WebUserControl1" TagPrefix="uc1" %>
<%@ Register Src="WebUserControl2.ascx" TagName="WebUserControl2" TagPrefix="uc2" %>

<!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>Manipulate Controls within DetailRow Tempate</title>
</head>
<body>
    <form id="form1" runat="server">
<%-- EndRegion --%>

<script type="text/javascript">
function DisableEditor(editor, e){
    editor.SetEnabled(false);
}
</script>

        <dxwgv:ASPxGridView ID="gvMaster" runat="server" DataSourceID="dsMaster" KeyFieldName="ID" OnRowInserting="GridView_RowInserting" AutoGenerateColumns="False" Width="300px" >
            <Templates>
                <DetailRow>

                    <dxtc:ASPxPageControl ID="ASPxPageControl1" runat="server" OnInit="ASPxPageControl1_DataBound" >
                        <TabPages>
                            <dxtc:TabPage Text="Description">
                                <ContentCollection>
                                    <dxw:ContentControl runat="server">
                                        <uc1:WebUserControl1 ID="WebUserControl1" runat="server" />
                                    </dxw:ContentControl>
                                </ContentCollection>
                            </dxtc:TabPage>
                            <dxtc:TabPage Text="Detail Data">
                                <ContentCollection>
                                    <dxw:ContentControl runat="server">
                                        <uc2:WebUserControl2 ID="WebUserControl2" runat="server">
                                        </uc2:WebUserControl2>                        
                                    </dxw:ContentControl>
                                </ContentCollection>
                            </dxtc:TabPage>
                        </TabPages>
                    </dxtc:ASPxPageControl>

                </DetailRow>
            </Templates>
            <SettingsDetail ShowDetailRow="True" />
            <Columns>
<%-- BeginRegion Command Column --%>
                <dxwgv:GridViewCommandColumn VisibleIndex="0" ShowNewButton="True"/>
<%-- EndRegion --%>                  
<%-- BeginRegion ID Column --%>
                <dxwgv:GridViewDataTextColumn FieldName="ID" ReadOnly="True" VisibleIndex="1">
                    <PropertiesTextEdit NullText="(Auto generated)">
                        <ClientSideEvents Init="DisableEditor" />
                    </PropertiesTextEdit>
                </dxwgv:GridViewDataTextColumn>
<%-- EndRegion --%>                
<%-- BeginRegion Data Column --%>                
                <dxwgv:GridViewDataTextColumn FieldName="Data" VisibleIndex="2">
                    <PropertiesTextEdit>
                        <ValidationSettings Display="Dynamic" SetFocusOnError="True">
                            <RequiredField IsRequired="True" />
                        </ValidationSettings>
                        <ClientSideEvents Init="function(s, e){s.Focus();}" />
                    </PropertiesTextEdit>
                </dxwgv:GridViewDataTextColumn>
<%-- EndRegion --%>                
            </Columns>
            <SettingsEditing EditFormColumnCount="1" />
        </dxwgv:ASPxGridView>

<%-- BeginRegion Master Data Source --%>
        <asp:ObjectDataSource ID="dsMaster" runat="server" SelectMethod="GetMasterRows" TypeName="GridDataSource" >
        </asp:ObjectDataSource>       
<%-- EndRegion --%>

<%-- BeginRegion Page ending tags --%>
    </form>
</body>
</html>
<%-- EndRegion --%>
ascx
<%-- BeginRegion Page starting tags --%>
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl2.ascx.cs" Inherits="WebUserControl2" %>

<%@ Register Assembly="DevExpress.Web.v15.1, Version=15.1.2.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
    Namespace="DevExpress.Web" TagPrefix="dxwgv" %>
<%@ Register Assembly="DevExpress.Web.v15.1, Version=15.1.2.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
    Namespace="DevExpress.Web" TagPrefix="dxe" %>
<%-- EndRegion --%>

<dxwgv:ASPxGridView ID="gvDetail" runat="server" DataSourceID="dsDetail" KeyFieldName="ID" OnBeforePerformDataSelect="gvDetail_BeforePerformDataSelect" AutoGenerateColumns="False" OnInitNewRow="gvDetail_InitNewRow" OnCellEditorInitialize="gvDetail_CellEditorInitialize" OnRowInserting="GridView_RowInserting">
    <SettingsDetail IsDetailGrid="True" />
    <Columns>
<%-- BeginRegion Command Column --%>
        <dxwgv:GridViewCommandColumn VisibleIndex="0" ShowNewButton="True"/>
<%-- EndRegion --%>  
<%-- BeginRegion ID Column --%>
        <dxwgv:GridViewDataTextColumn FieldName="ID" ReadOnly="True" VisibleIndex="1">
            <PropertiesTextEdit NullText="(Auto generated)">
                <ClientSideEvents Init="DisableEditor" />            
            </PropertiesTextEdit>        
        </dxwgv:GridViewDataTextColumn>
<%-- EndRegion --%>  
<%-- BeginRegion MasterID Column --%>        
        <dxwgv:GridViewDataTextColumn FieldName="MasterID" ReadOnly="True" VisibleIndex="2">
            <PropertiesTextEdit>
                <ClientSideEvents Init="DisableEditor" />
            </PropertiesTextEdit>
        </dxwgv:GridViewDataTextColumn>
<%-- EndRegion --%>  
<%-- BeginRegion DetailData Column --%>
        <dxwgv:GridViewDataTextColumn FieldName="DetailData" VisibleIndex="3">
            <EditFormSettings Caption="Detail&amp;nbsp;Data" />
            <PropertiesTextEdit>
                <ValidationSettings Display="Dynamic" SetFocusOnError="True">
                    <RequiredField IsRequired="True" />
                </ValidationSettings>
                <ClientSideEvents Init="function(s, e){s.Focus();}" />
            </PropertiesTextEdit>
        </dxwgv:GridViewDataTextColumn>
<%-- EndRegion --%>  
    </Columns>
    <SettingsEditing EditFormColumnCount="1" />
</dxwgv:ASPxGridView> 

<%-- BeginRegion Detail Data Source --%>
<asp:ObjectDataSource ID="dsDetail" runat="server" SelectMethod="GetDetailRows" TypeName="GridDataSource">
    <SelectParameters>
        <asp:SessionParameter Name="masterId" SessionField="MasterID" Type="Int32" />
    </SelectParameters>
</asp:ObjectDataSource>
<%-- EndRegion --%>
csharp
using System;

public partial class WebUserControl1 : System.Web.UI.UserControl {
    protected void Page_Load(object sender, EventArgs e) {

    }
}
csharp
using System;
using System.Data;
using System.Web;
using System.Web.SessionState;
using System.ComponentModel;
using System.Collections;
using System.Collections.Specialized;

public class GridDataSource {
    HttpSessionState Session { get { return HttpContext.Current.Session; } }

    public DataTable GetMasterRows(){
        return GetDataSource().Tables["MasterTable"] as DataTable;
    }

    public DataView GetDetailRows(int masterId){
        DataTable detailTable = GetDataSource().Tables["DetailTable"];
        DataView dataView = new DataView(detailTable);
        dataView.RowFilter = "MasterID = " + Session["MasterID"].ToString();
        return dataView;
    }

    public void InsertRow(OrderedDictionary newValues, bool isDetail){
        DataTable dataTable = GetDataTable(isDetail);
        DataRow row = dataTable.NewRow();
        newValues["ID"] = dataTable.Rows.Count;
        IDictionaryEnumerator enumerator = newValues.GetEnumerator();
        enumerator.Reset();
        while (enumerator.MoveNext())
            if (enumerator.Key.ToString() != "Count")
                row[enumerator.Key.ToString()] = enumerator.Value;
        if (!isDetail) row["Description"] = "Auto generated description for Master Row " + newValues["ID"].ToString();
        dataTable.Rows.Add(row);
    }

    private DataTable GetDataTable(bool isDetail) {
        return isDetail ? GetDataSource().Tables["DetailTable"] : GetDataSource().Tables["MasterTable"];
    }

    private DataSet GetDataSource(){
        if (Session["GridDataSource"] == null) CreateGridDataSource();
        return Session["GridDataSource"] as DataSet;
    }

    private void CreateGridDataSource(){
        //Create a master table's structure
        DataTable masterTable = new DataTable("MasterTable");
        masterTable.Columns.Add("ID", typeof(int));
        masterTable.Columns.Add("Data", typeof(string));
        masterTable.Columns.Add("Description", typeof(string));
        masterTable.PrimaryKey = new DataColumn[] { masterTable.Columns["ID"] };

        //Create a detail table's structure
        DataTable detailTable = new DataTable("DetailTable");
        detailTable.Columns.Add("ID", typeof(int));
        detailTable.Columns.Add("MasterID", typeof(int));
        detailTable.Columns.Add("DetailData", typeof(string));
        detailTable.PrimaryKey = new DataColumn[] { detailTable.Columns["ID"] };

        //Populate data tables
        int index = 0;
        for (int i = 0; i < 15; i++){
            masterTable.Rows.Add(new object[] { i, "Master Row " + i.ToString(), "Description for Master Row " + i.ToString() });
            for (int j = 0; j < 5; j++)
                detailTable.Rows.Add(new object[] { index++, i, "Detail Row " + j.ToString() });
        }

        //Add tables to a data source, and store it within a Session 
        DataSet ds = new DataSet();
        ds.Tables.AddRange(new DataTable[] { masterTable, detailTable });
        Session["GridDataSource"] = ds;
    }
}
ascx
<%-- BeginRegion Page starting tags --%>
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl1.ascx.cs" Inherits="WebUserControl1" %>
<%@ Register Assembly="DevExpress.Web.v15.1, Version=15.1.2.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
    Namespace="DevExpress.Web" TagPrefix="dxe" %>
<%-- EndRegion --%>

<dxe:ASPxLabel ID="ASPxLabel1" runat="server" Text="Master Row Data Description:" >
</dxe:ASPxLabel>


<dxe:ASPxMemo ID="ASPxMemo1" runat="server" Height="50px" Width="150px" ReadOnly="True" Text='<%# Eval("Description") %>'>
</dxe:ASPxMemo>
csharp
using System;
using DevExpress.Web;

public partial class WebUserControl2 : System.Web.UI.UserControl {
    protected void Page_Load(object sender, EventArgs e) {

    }

    protected void gvDetail_BeforePerformDataSelect(object sender, EventArgs e){
        Session["MasterID"] = (sender as ASPxGridView).GetMasterRowKeyValue();
    }
    protected void gvDetail_InitNewRow(object sender, DevExpress.Web.Data.ASPxDataInitNewRowEventArgs e){
        Session["MasterID"] = (sender as ASPxGridView).GetMasterRowKeyValue();
        e.NewValues["MasterID"] = Session["MasterID"].ToString();
    }

    protected void GridView_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e){
        ASPxGridView gridView = (ASPxGridView)sender;

        GridDataSource gridDataSource = new GridDataSource();
        gridDataSource.InsertRow(e.NewValues, gridView.SettingsDetail.IsDetailGrid);

        gridView.CancelEdit();
        e.Cancel = true;
    }
    protected void gvDetail_CellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e) {
        ASPxGridView gridView = (ASPxGridView)sender;
        if (e.Column.FieldName == "MasterID") {
            ASPxTextBox textBoxMasterID = (ASPxTextBox)e.Editor;
            if (textBoxMasterID.Value == null)
                textBoxMasterID.Value = gridView.GetMasterRowKeyValue();
        }
    }
}
vb
Imports Microsoft.VisualBasic
Imports System

Partial Public Class WebUserControl1
    Inherits System.Web.UI.UserControl
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    End Sub
End Class
aspx
<%-- BeginRegion Page starting tags --%>

<%@ Page Language="vb" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<%@ Register Assembly="DevExpress.Web.v15.1, Version=15.1.2.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
    Namespace="DevExpress.Web" TagPrefix="dxwgv" %>
<%@ Register Assembly="DevExpress.Web.v15.1, Version=15.1.2.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
    Namespace="DevExpress.Web" TagPrefix="dxe" %>

<%@ Register Assembly="DevExpress.Web.v15.1, Version=15.1.2.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
    Namespace="DevExpress.Web" TagPrefix="dxw" %>
<%@ Register Assembly="DevExpress.Web.v15.1, Version=15.1.2.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
    Namespace="DevExpress.Web" TagPrefix="dxtc" %>

<%@ Register Src="WebUserControl1.ascx" TagName="WebUserControl1" TagPrefix="uc1" %>
<%@ Register Src="WebUserControl2.ascx" TagName="WebUserControl2" TagPrefix="uc2" %>

<!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>Manipulate Controls within DetailRow Tempate</title>
</head>
<body>
    <form id="form1" runat="server">
<%-- EndRegion --%>

        <script type="text/javascript">
            function DisableEditor(editor, e){
                editor.SetEnabled(false);
            }
        </script>

        <dxwgv:ASPxGridView ID="gvMaster" runat="server" DataSourceID="dsMaster" KeyFieldName="ID" OnRowInserting="GridView_RowInserting" AutoGenerateColumns="False" Width="300px" >
            <Templates>
                <DetailRow>

                    <dxtc:ASPxPageControl ID="ASPxPageControl1" runat="server" OnInit="ASPxPageControl1_DataBound" >
                        <TabPages>
                            <dxtc:TabPage Text="Description">
                                <ContentCollection>
                                    <dxw:ContentControl runat="server">
                                        <uc1:WebUserControl1 ID="WebUserControl1" runat="server" />
                                    </dxw:ContentControl>
                                </ContentCollection>
                            </dxtc:TabPage>
                            <dxtc:TabPage Text="Detail Data">
                                <ContentCollection>
                                    <dxw:ContentControl runat="server">
                                        <uc2:WebUserControl2 ID="WebUserControl2" runat="server">
                                        </uc2:WebUserControl2>                        
                                    </dxw:ContentControl>
                                </ContentCollection>
                            </dxtc:TabPage>
                        </TabPages>
                    </dxtc:ASPxPageControl>

                </DetailRow>
            </Templates>
            <SettingsDetail ShowDetailRow="True" />
            <Columns>
<%-- BeginRegion Command Column --%>
                <dxwgv:GridViewCommandColumn VisibleIndex="0" ShowNewButton="True"/>
<%-- EndRegion --%>                  
<%-- BeginRegion ID Column --%>
                <dxwgv:GridViewDataTextColumn FieldName="ID" ReadOnly="True" VisibleIndex="1">
                    <PropertiesTextEdit NullText="(Auto generated)">
                        <ClientSideEvents Init="DisableEditor" />
                    </PropertiesTextEdit>
                </dxwgv:GridViewDataTextColumn>
<%-- EndRegion --%>                
<%-- BeginRegion Data Column --%>                
                <dxwgv:GridViewDataTextColumn FieldName="Data" VisibleIndex="2">
                    <PropertiesTextEdit>
                        <ValidationSettings Display="Dynamic" SetFocusOnError="True">
                            <RequiredField IsRequired="True" />
                        </ValidationSettings>
                        <ClientSideEvents Init="function(s, e){s.Focus();}" />
                    </PropertiesTextEdit>
                </dxwgv:GridViewDataTextColumn>
<%-- EndRegion --%>                
            </Columns>
            <SettingsEditing EditFormColumnCount="1" />
        </dxwgv:ASPxGridView>

<%-- BeginRegion Master Data Source --%>
        <asp:ObjectDataSource ID="dsMaster" runat="server" SelectMethod="GetMasterRows" TypeName="GridDataSource" >
        </asp:ObjectDataSource>       
<%-- EndRegion --%>

<%-- BeginRegion Page ending tags --%>
    </form>
</body>
</html>
<%-- EndRegion --%>
vb
Imports Microsoft.VisualBasic
Imports System
Imports DevExpress.Web

Partial Public Class WebUserControl2
    Inherits System.Web.UI.UserControl
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    End Sub

    Protected Sub gvDetail_BeforePerformDataSelect(ByVal sender As Object, ByVal e As EventArgs)
        Session("MasterID") = (TryCast(sender, ASPxGridView)).GetMasterRowKeyValue()
    End Sub
    Protected Sub gvDetail_InitNewRow(ByVal sender As Object, ByVal e As DevExpress.Web.Data.ASPxDataInitNewRowEventArgs)
                                Session("MasterID") = (TryCast(sender, ASPxGridView)).GetMasterRowKeyValue()
        e.NewValues("MasterID") = Session("MasterID").ToString()
    End Sub

    Protected Sub GridView_RowInserting(ByVal sender As Object, ByVal e As DevExpress.Web.Data.ASPxDataInsertingEventArgs)
        Dim gridView As ASPxGridView = CType(sender, ASPxGridView)

        Dim gridDataSource As New GridDataSource()
        gridDataSource.InsertRow(e.NewValues, gridView.SettingsDetail.IsDetailGrid)

        gridView.CancelEdit()
        e.Cancel = True
    End Sub
    Protected Sub gvDetail_CellEditorInitialize(ByVal sender As Object, ByVal e As ASPxGridViewEditorEventArgs)
        Dim gridView As ASPxGridView = CType(sender, ASPxGridView)
        If e.Column.FieldName = "MasterID" Then
            Dim textBoxMasterID As ASPxTextBox = CType(e.Editor, ASPxTextBox)
            If textBoxMasterID.Value Is Nothing Then
                textBoxMasterID.Value = gridView.GetMasterRowKeyValue()
            End If
        End If
    End Sub
End Class
vb
Imports Microsoft.VisualBasic
Imports System
Imports DevExpress.Web

Partial Public Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    End Sub

    Protected Sub GridView_RowInserting(ByVal sender As Object, ByVal e As DevExpress.Web.Data.ASPxDataInsertingEventArgs)
        Dim gridView As ASPxGridView = CType(sender, ASPxGridView)

        Dim gridDataSource As New GridDataSource()
        gridDataSource.InsertRow(e.NewValues, gridView.SettingsDetail.IsDetailGrid)

        gridView.CancelEdit()
        e.Cancel = True

        'Navigate to the newly inserted row within the grid and open its details
        Dim newRowIndex As Integer = gridView.FindVisibleIndexByKeyValue(e.NewValues("ID"))

        gridView.PageIndex = newRowIndex / gridView.SettingsPager.PageSize
        gridView.DetailRows.ExpandRow(newRowIndex)

        activeTabIndex = 1
    End Sub

    Private activeTabIndex As Nullable(Of Integer)

    Protected Sub ASPxPageControl1_DataBound(ByVal sender As Object, ByVal e As EventArgs)
        If activeTabIndex.HasValue Then
            'Change the ASPxPageControl's active tab page, and switch the detail grid to insert mode
            Dim pageControl As ASPxPageControl = TryCast(sender, ASPxPageControl)
            pageControl.ActiveTabIndex = activeTabIndex.Value
            Dim detailGrid As ASPxGridView = TryCast(pageControl.FindControl("WebUserControl2").FindControl("gvDetail"), ASPxGridView)
            detailGrid.AddNewRow()
        End If
    End Sub
End Class
vb
Imports Microsoft.VisualBasic
Imports System
Imports System.Data
Imports System.Web
Imports System.Web.SessionState
Imports System.ComponentModel
Imports System.Collections
Imports System.Collections.Specialized

Public Class GridDataSource
    Private ReadOnly Property Session() As HttpSessionState
        Get
            Return HttpContext.Current.Session
        End Get
    End Property

    Public Function GetMasterRows() As DataTable
        Return TryCast(GetDataSource().Tables("MasterTable"), DataTable)
    End Function

    Public Function GetDetailRows(ByVal masterId As Integer) As DataView
        Dim detailTable As DataTable = GetDataSource().Tables("DetailTable")
        Dim dataView As New DataView(detailTable)
        dataView.RowFilter = "MasterID = " & Session("MasterID").ToString()
        Return dataView
    End Function

    Public Sub InsertRow(ByVal newValues As OrderedDictionary, ByVal isDetail As Boolean)
        Dim dataTable As DataTable = GetDataTable(isDetail)
        Dim row As DataRow = dataTable.NewRow()
        newValues("ID") = dataTable.Rows.Count
        Dim enumerator As IDictionaryEnumerator = newValues.GetEnumerator()
        enumerator.Reset()
        Do While enumerator.MoveNext()
            If enumerator.Key.ToString() <> "Count" Then
                row(enumerator.Key.ToString()) = enumerator.Value
            End If
        Loop
        If (Not isDetail) Then
            row("Description") = "Auto generated description for Master Row " & newValues("ID").ToString()
        End If
        dataTable.Rows.Add(row)
    End Sub

    Public Function GetRowCount(ByVal isDetail As Boolean) As Integer
        Return GetDataTable(isDetail).Rows.Count
    End Function

    Private Function GetDataTable(ByVal isDetail As Boolean) As DataTable
        If isDetail Then
            Return GetDataSource().Tables("DetailTable")
        Else
            Return GetDataSource().Tables("MasterTable")
        End If
    End Function

    Private Function GetDataSource() As DataSet
        If Session("GridDataSource") Is Nothing Then
            CreateGridDataSource()
        End If
        Return TryCast(Session("GridDataSource"), DataSet)
    End Function

    Private Sub CreateGridDataSource()
        'Create a master table's structure
        Dim masterTable As New DataTable("MasterTable")
        masterTable.Columns.Add("ID", GetType(Integer))
        masterTable.Columns.Add("Data", GetType(String))
        masterTable.Columns.Add("Description", GetType(String))
        masterTable.PrimaryKey = New DataColumn() { masterTable.Columns("ID") }

        'Create a detail table's structure
        Dim detailTable As New DataTable("DetailTable")
        detailTable.Columns.Add("ID", GetType(Integer))
        detailTable.Columns.Add("MasterID", GetType(Integer))
        detailTable.Columns.Add("DetailData", GetType(String))
        detailTable.PrimaryKey = New DataColumn() { detailTable.Columns("ID") }

        'Populate data tables
        Dim index As Integer = 0
        For i As Integer = 0 To 14
            masterTable.Rows.Add(New Object() { i, "Master Row " & i.ToString(), "Description for Master Row " & i.ToString() })
            For j As Integer = 0 To 4
                detailTable.Rows.Add(New Object() { index, i, "Detail Row " & j.ToString() })
                index += 1
            Next j
        Next i
        'Add tables to a data source, and store it within a Session 
        Dim ds As New DataSet()
        ds.Tables.AddRange(New DataTable() { masterTable, detailTable })
        Session("GridDataSource") = ds
    End Sub
End Class
ascx
<%-- BeginRegion Page starting tags --%>
<%@ Control Language="vb" AutoEventWireup="true" CodeFile="WebUserControl2.ascx.vb" Inherits="WebUserControl2" %>

<%@ Register Assembly="DevExpress.Web.v15.1, Version=15.1.2.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
    Namespace="DevExpress.Web" TagPrefix="dxwgv" %>
<%@ Register Assembly="DevExpress.Web.v15.1, Version=15.1.2.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
    Namespace="DevExpress.Web" TagPrefix="dxe" %>
<%-- EndRegion --%>

<dxwgv:ASPxGridView ID="gvDetail" runat="server" DataSourceID="dsDetail" KeyFieldName="ID" OnBeforePerformDataSelect="gvDetail_BeforePerformDataSelect" AutoGenerateColumns="False" OnInitNewRow="gvDetail_InitNewRow" OnCellEditorInitialize="gvDetail_CellEditorInitialize" OnRowInserting="GridView_RowInserting">
    <SettingsDetail IsDetailGrid="True" />
    <Columns>
<%-- BeginRegion Command Column --%>
        <dxwgv:GridViewCommandColumn VisibleIndex="0" ShowNewButton="True"/>
<%-- EndRegion --%>  
<%-- BeginRegion ID Column --%>
        <dxwgv:GridViewDataTextColumn FieldName="ID" ReadOnly="True" VisibleIndex="1">
            <PropertiesTextEdit NullText="(Auto generated)">
                <ClientSideEvents Init="DisableEditor" />            
            </PropertiesTextEdit>        
        </dxwgv:GridViewDataTextColumn>
<%-- EndRegion --%>  
<%-- BeginRegion MasterID Column --%>        
        <dxwgv:GridViewDataTextColumn FieldName="MasterID" ReadOnly="True" VisibleIndex="2">
            <PropertiesTextEdit>
                <ClientSideEvents Init="DisableEditor" />
            </PropertiesTextEdit>
        </dxwgv:GridViewDataTextColumn>
<%-- EndRegion --%>  
<%-- BeginRegion DetailData Column --%>
        <dxwgv:GridViewDataTextColumn FieldName="DetailData" VisibleIndex="3">
            <EditFormSettings Caption="Detail&amp;nbsp;Data" />
            <PropertiesTextEdit>
                <ValidationSettings Display="Dynamic" SetFocusOnError="True">
                    <RequiredField IsRequired="True" />
                </ValidationSettings>
                <ClientSideEvents Init="function(s, e){s.Focus();}" />
            </PropertiesTextEdit>
        </dxwgv:GridViewDataTextColumn>
<%-- EndRegion --%>  
    </Columns>
    <SettingsEditing EditFormColumnCount="1" />
</dxwgv:ASPxGridView> 

<%-- BeginRegion Detail Data Source --%>
<asp:ObjectDataSource ID="dsDetail" runat="server" SelectMethod="GetDetailRows" TypeName="GridDataSource">
    <SelectParameters>
        <asp:SessionParameter Name="masterId" SessionField="MasterID" Type="Int32" />
    </SelectParameters>
</asp:ObjectDataSource>
<%-- EndRegion --%>
ascx
<%-- BeginRegion Page starting tags --%>
<%@ Control Language="vb" AutoEventWireup="true" CodeFile="WebUserControl1.ascx.vb" Inherits="WebUserControl1" %>
<%@ Register Assembly="DevExpress.Web.v15.1, Version=15.1.2.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
    Namespace="DevExpress.Web" TagPrefix="dxe" %>
<%-- EndRegion --%>

<dxe:ASPxLabel ID="ASPxLabel1" runat="server" Text="Master Row Data Description:" >
</dxe:ASPxLabel>


<dxe:ASPxMemo ID="ASPxMemo1" runat="server" Height="50px" Width="150px" ReadOnly="True" Text='<%#Eval("Description")%>'>
</dxe:ASPxMemo>

See Also

Mode

Grid View

ASPxGridView Class

ASPxGridView Members

DevExpress.Web Namespace