Reputation: 17492
Can anyone explan how I can access the selected row of a detail grid in a DevExpress master-detail ASPxGridView? I've found an example on the devexpress support website But I can't get it tow ork, I'm working with version 11 of DevExpress.
Thanks in advance.
Upvotes: 2
Views: 20570
Reputation: 131
Thanks in advance to user189756 answer because it is helpful but I suppose many people are heading into the same issue here and because the previous answer is not up to date for current versions of DevExpress Asp.Net WebForms since it was written almost 5 years ago I just wanted to add an important point here. In order to process the Selection Event on server side now you must specify it in ASPxGridView attributes as follows:
<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>
Notice I used the row selection by click but there is also another variant using checkboxes. So now the only thing you have to do is implement Selection Event Handler in code behind.
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;
}
Upvotes: 0
Reputation: 17492
I found a way to get the selected row of the detail grid, not sure how 'advised' it is to do it this way but it works fine for me, I added an onload()
event to the detail grid and then I was able to access that instance of the gridview by casting it to an ASPxGridView.
Here is my Code, the detail grid:
<Templates>
<DetailRow>
<dx:ASPxGridView ID="detailGrid" runat="server" DataSourceID="SqlDataSource2"
Width="100%" OnBeforePerformDataSelect="detailGrid_DataSelect"
KeyFieldName="InvoiceID"
EnableCallBacks="False"
onload="detailGrid_Load"
>
and then I handle the onoad()
event like this:
ASPxGridView gridView;
protected void detailGrid_Load(object sender, EventArgs e)
{
gridView = sender as ASPxGridView;
gridView.SelectionChanged += new EventHandler(gridView_SelectionChanged);
}
So I just made a ASPxGridView instance of the detail grid, and now I can make use of its SelectionChanged()
event.
private static int invoiceID;
void gridView_SelectionChanged(object sender, EventArgs e)
{
invoiceID = Convert.ToInt64(gridView.GetSelectedFieldValues("InvoiceID")[0]);
}
Upvotes: 3