Category / Section
How to add checkbox and manipulate checked cells in OLAP Grid?
1 min read
You can add checkbox for row headers except column headers and summary by using the following code in RowBound event.
C#
protected void Page_Init(object sender, EventArgs e) { this.OlapGrid1.RowBound += new EventHandler<RowBoundEventArgs>(OlapGrid1_RowBound); } //RowBound event to add the checkbox control for each record except the headers and summary row. void OlapGrid1_RowBound(object sender, RowBoundEventArgs e) { e.GridRow.Cells.AddAt(0, new TableCell()); var pivotCell = e.GridRow.Cells[1] as PivotGridCell; if (pivotCell.CellDescriptor.CellType.In(PivotCellDescriptorType.RowHeader)) { e.GridRow.Cells[0].Controls.Add(new CheckBox() { ID = "chk" + pivotCell.ID }); } }
VB
Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs) AddHandler OlapGrid1.RowBound, AddressOf OlapGrid1_RowBound End Sub 'RowBound event to add the checkbox control for each record except the headers and summary row. Private Sub OlapGrid1_RowBound(ByVal sender As Object, ByVal e As RowBoundEventArgs) e.GridRow.Cells.AddAt(0, New TableCell()) Dim pivotCell = TryCast(e.GridRow.Cells(1), PivotGridCell) If pivotCell.CellDescriptor.CellType.In(PivotCellDescriptorType.RowHeader) Then e.GridRow.Cells(0).Controls.Add(New CheckBox() With {.ID = "chk" & pivotCell.ID}) End If End Sub
You can get the rows whose checkbox has been checked by using the following code example.
C#
//To get the entire row whose checkbox is checked. Selected rows contains the entire row which has been with the checkbox checked. List<PivotRowDescriptor> selectedRows = new List<PivotRowDescriptor>(); var table = this.OlapGrid1.FindControl("PivotTable") as Table; foreach (OlapGridRow item in table.Rows) { if (item.Cells[0].Controls.Count == 0) continue; var chk = item.Cells[0].Controls[0] as CheckBox; if (chk.Checked) selectedRows.Add(item.RowDescriptor); } selectedRows.ToArray();
VB
'To get the entire row whose checkbox is checked. Selected rows contains the entire row which has been with the checkbox checked. Dim selectedRows As List(Of PivotRowDescriptor) = New List(Of PivotRowDescriptor)() Dim table = TryCast(Me.OlapGrid1.FindControl("PivotTable"), Table) For Each item As OlapGridRow In table.Rows If item.Cells(0).Controls.Count = 0 Then Continue For End If Dim chk = TryCast(item.Cells(0).Controls(0), CheckBox) If chk.Checked Then selectedRows.Add(item.RowDescriptor) End If Next item selectedRows.ToArray()
All the selected rows are displayed within the selectedRows array, through which you can get cell values of the selected rows.
C#
//To get the column cells, selectedCells contains all column cells. var selectedCells = new List<PivotCellDescriptor>(); for (int i = 0; i < selectedRows.Count; i++) { foreach (PivotCellDescriptor cell in selectedRows[i].Cells) { if (cell.CellType == PivotCellDescriptorType.Value && cell.CellData.ColumnInfo.Count != 0) { selectedCells.Add(cell); } } }
VB
'To get the column cells, selectedCells contains all column cells. Dim selectedCells = New List(Of PivotCellDescriptor)() For i As Integer = 0 To selectedRows.Count - 1 For Each cell As PivotCellDescriptor In selectedRows(i).Cells If cell.CellType Is PivotCellDescriptorType.Value AndAlso cell.CellData.ColumnInfo.Count <> 0 Then selectedCells.Add(cell) End If Next cell Next i
In the above code example, selectedCells contains all the cell values of the checked rows and using these values you can do any processing based on the users requirement.
Note:This scenario will work only when we set IsAsyncRequestData="false" in OlapGrid.