【问题标题】:ASPxGridView - How to get the Selected Row in a Detail Grid of a Master-Detail GridView?ASPxGridView - 如何在主-详细信息 GridView 的详细信息网格中获取选定行?
【发布时间】:2011-12-28 06:54:53
【问题描述】:

谁能解释我如何访问DevExpress master-detail ASPxGridView? 中选定的详细信息网格行我找到了an example on the devexpress support website 但我无法得到它,我正在使用 DevExpress 的第 11 版。

提前致谢。

【问题讨论】:

  • 您希望在哪个事件中获取详细网格中的选定行?
  • 最好在详细信息网格的 SelectionChanged() 事件中,但我什至无法从后面的代码访问我的详细信息网格,因此我无法真正使用该事件。
  • 究竟是什么不起作用?事件未触发或...?你能发布你的代码吗?
  • >>但我什至无法从后面的代码访问我的详细信息网格。请分享您的代码。
  • 为了能够访问后面代码中的详细信息网格,只需在加载后将其转换为 ASPxGridView,请在下面查看我的解决方案。

标签: asp.net devexpress aspxgridview master-detail


【解决方案1】:

我找到了一种获取详细信息网格的选定行的方法,不确定这样做有多“建议”,但它对我来说很好,我在详细信息网格中添加了一个 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]);
    }

【讨论】:

    【解决方案2】:

    提前感谢 user189756 的回答,因为它很有帮助,但我想很多人在这里遇到同样的问题,因为之前的回答对于 DevExpress Asp.Net WebForms 的当前版本不是最新的,因为它几乎是 5几年前,我只是想在这里补充一个重要的观点。 为了在服务器端处理选择事件,您必须在 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>
    

    请注意,我通过单击使用行选择,但还有另一个使用复选框的变体。所以现在你唯一要做的就是在后面的代码中实现选择事件处理程序。

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-02
      • 2012-03-29
      • 2010-12-06
      • 1970-01-01
      相关资源
      最近更新 更多