I have a web grid in the page. When user selects any row (in the normal scenario) in the grid by double click on selected row, I display the data related with this record in a form under the grid. But when I group by some column in the grid, and double click on any row of the child (nested) grid to display the data in the form, I can't identify the row where I am and nothing appears in the form.
How can I identify the selected row in a grouped by grid in a WebGrid ?
Thanks to any idea.
This is the code:
Private Sub uwgrdOC_DblClick(ByVal sender As Object, ByVal e As Infragistics.WebUI.UltraWebGrid.ClickEventArgs) Handles uwgrdOC.DblClick
Try
Call CaragarDetalleOC(CInt(Me.uwgrdOC.Rows(e.Row.Index).Cells.FromKey("ID").Text))
Catch ex As Exception
End Try
End Sub
Private Sub ConfigurarGrid(ByRef UltraGrid As Infragistics.WebUI.UltraWebGrid.UltraWebGrid)
UltraGrid.DisplayLayout.RowSelectorsDefault = Infragistics.WebUI.UltraWebGrid.RowSelectors.Yes
UltraGrid.DisplayLayout.CellClickActionDefault = Infragistics.WebUI.UltraWebGrid.CellClickAction.CellSelect
UltraGrid.DisplayLayout.SelectTypeCellDefault = Infragistics.WebUI.UltraWebGrid.SelectType.Extended
UltraGrid.DisplayLayout.SelectTypeRowDefault = Infragistics.WebUI.UltraWebGrid.SelectType.Extended
When you group the grid, your data rows are no longer direclty in the grid's Rows collection. Instead, this contains one or more GroupByRow objects, one for each group. Each GroupByRow object either contains another set of GroupByRow objects (if you have multiple levels of grouping) or the UltraGridRow objects that represent your data.
This means that you can't retrieve the row by its index. The "index" will represent where the double-clicked-on row exists in its parent collection, and won't match what's in the grid.
To identify the row, you may be able to make use of the ClickEventArgs of the DblClick event. Take a look at e.Row - if this is not null, then you've double-clicked on a row. So, I'd replace the Try block in your statement with the following few lines:
If Not e.Row Is Nothing Then Try Call CaragarDetalleOC(CInt(e.RowCells.FromKey("ID").Text)) Catch ex as Exception End TryEnd If
Hi!
I have a very similar problem. I however, expect one or many rows to be selected on my postback. Since I haven't found any SelectedRows collection I unfortunately iterate through every single row in the Rows collection to check if they are Selected. This is probably not the best or preferred way to do it?
for (int i = 0; i < grd.Rows.Count; i++){ if (grd.Rows[i].Selected) { // Here goes my stuff }}
In the light of your above explanation I realise it won't work at all when the user have grouped by. So I thought I'd expand on my above code to do something like:
for (int i = 0; i < grd.GroupByRows.Count; i++){ for (int j = 0; j < grd.GroupByRows[i].Rows.Count; j++) { if (grd.GroupByRows[i].Rows[j].Selected) { // Here goes my stuff } }}
But since there was no GroupByRows collection I don't now what to do.
Any pointers to a better solution would be much appreciated.
regards,
!Rob
There is no "group-by rows" collection, because the rows in the grid's Rows collection are group-by rows.
What you're likely looking for is the grid's DisplayLayout.SelectedRows collection.
Knowing how to loop through all your rows when your grid is grouped may be helpful, however. Since you can have any arbitrary level of grouping, this is best done by making a recursive method. Here's an example based on what you've written; please note that I'm currently unable to test this example.
using Infragistics.WebUI.UltraWebGrid;...private void DoSomethingOnSelectedRows(){ // Pass the grid's Rows collection to the overload that takes a Rows collection DoSomethingOnSelectedRows(grd.Rows);}private void DoSomethingOnSelectedRows(RowsCollection rows){ foreach (UltraGridRow row in rows) { // See if this is a group-by row GroupByRow groupRow = row as GroupByRow; if (groupRow == null) { // Not a group-by row // See if the row is selected if (row.Selected) { // Do something } } else { // This is a group-by row; loop through its Rows collection DoSomethingOnSelectedRows(groupRow.Rows); } }}
Alternately, you could use an UltraGridRowEnumerator to enumerate through all rows in the grid, and simply ignore group-by rows.
Thank you very much for that!
This will add heaps of performance to my code. I cannot believe I hadn't found DisplayLayout.SelectedRows.
Although, it is probably not the most intuitive place for it to sit. Never mind though, I am very happy to have learned about it.
Thanks again,