ASPxGridView - 如何在Master-Detail GridView的详细网格中获取所选行?

时间:2011-12-28 06:54:53

标签: asp.net devexpress aspxgridview master-detail

任何人都可以解释我如何访问DevExpress master-detail ASPxGridView?我发现an example on the devexpress support website {{}}}中详细网格的选定行但我无法得到它,我正在使用版本11 DevExpress。

提前致谢。

2 个答案:

答案 0 :(得分:3)

我找到了一种方法来获取详细网格的选定行,不知道这样做是怎么'建议'但它对我来说很好,我在细节网格中添加了onload()事件然后我可以通过将其转换为ASPxGridView来访问gridview的该实例。

这是我的代码,详细网格:

<Templates>
            <DetailRow>

                <dx:ASPxGridView ID="detailGrid" runat="server"   DataSourceID="SqlDataSource2" 
                    Width="100%" OnBeforePerformDataSelect="detailGrid_DataSelect" 
                         KeyFieldName="InvoiceID"
                         EnableCallBacks="False" 
                         onload="detailGrid_Load"
                          >

然后我像这样处理onoad()事件:

ASPxGridView gridView;
protected void detailGrid_Load(object sender, EventArgs e)
{

    gridView = sender as ASPxGridView;
    gridView.SelectionChanged += new EventHandler(gridView_SelectionChanged);

}

所以我刚刚创建了一个详细网格的ASPxGridView实例,现在我可以使用它的SelectionChanged()事件了。

private static int invoiceID;

    void gridView_SelectionChanged(object sender, EventArgs e)
    {
        invoiceID = Convert.ToInt64(gridView.GetSelectedFieldValues("InvoiceID")[0]);
    }

答案 1 :(得分:0)

提前感谢user189756回答,因为它很有帮助,但我想很多人都在这里遇到同样的问题,因为之前的答案对于当前版本的DevExpress Asp.Net WebForms来说并不是最新的,因为它写得差不多5几年前我只想在这里补充一点。 现在,为了在服务器端处理Selection事件,您必须在ASPxGridView属性中指定它,如下所示:

<dx:ASPxGridView ID="MainGrid" runat="server">
    <Columns>
        <!-- Grid Columns here -->
    </Columns>
    <Templates>
        <DetailRow>
            <dx:ASPxGridView ID="DetailGrid" runat="server" KeyFieldName="ID" OnInit="Grid_Init" OnSelectionChanged="Grid_SelectionChanged">
                <Columns>
                    <!-- Grid Columns here -->
                </Columns> 
                <!-- Now the following code is relevant to process Selection Event on Server Side-->
                <SettingsBehavior AllowFocusedRow="true"
                    AllowSelectByRowClick="true"
                    ProcessFocusedRowChangedOnServer="true"
                    ProcessSelectionChangedOnServer="true"/>
                <SettingsDetail IsDetailGrid="True" />
            </dx:ASPxGridView>
        </DetailRow>
    </Templates>
    <SettingsDetail ShowDetailRow="True" />
</dx:ASPxGridView>

注意我通过点击使用了行选择,但是还有另一个使用复选框的变体。所以现在你唯一需要做的就是在后面的代码中实现Selection Event Handler。

protected void Grid_SelectionChanged(object sender, EventArgs e)
{
    ASPxGridView grid = sender as ASPxGridView;
    for (int i = 0; i < grid.VisibleRowCount; i++) // Loop through selected rows 
    {
        if (grid.Selection.IsRowSelected(i)) // do whatever you need to do with selected row values
        {
// now use pre-initialized List<object> selectedList to save 
            selectedList.Add(Convert.ToInt32(grid.GetRowValues(i, "ID")));
        }
    }
    ViewState["SelectedList"] = selectedList;
}