How to change the cell value in OLAP Grid?
This KB illustrates that how to change the cell value in OLAP Grid.
Solution
You can change the cell value in the OlapGrid in two ways.
- By using the QueryCellInfo event of the OlapGrid control.
- By editing the DOM element.
Using the QueryCellInfo event
The QueryCellInfo event gives two parameters, CellDescriptor and Cell. The CellDescriptor parameter helps you to select the position of the cell and the Cell parameter helps you to change the cell value. By using the Text property in the Cell parameter, you can change the cell content.
This event is invoked every time a new cell is created.
The following code example illustrates how to set or change the cell value of the OlapGrid by using the QueryCellInfo event.
C#
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { } this.OlapClient1.OlapGrid.QueryCellInfo+=OlapGrid_QueryCellInfo; } private void OlapGrid_QueryCellInfo(object sender, CellInfoEventArgs e) { if (e.CellDescriptor.PivotRowIndex < 2 && e.CellDescriptor.CellIndex > 2) e.Cell.Text = "Your value here"; }
VB
Protected Sub Page_Load(sender As Object, e As EventArgs) If Not IsPostBack Then End If AddHandler Me.OlapClient1.OlapGrid.QueryCellInfo, AddressOf OlapGrid_QueryCellInfo End Sub Private Sub OlapGrid_QueryCellInfo(sender As Object, e As CellInfoEventArgs) If e.CellDescriptor.PivotRowIndex < 2 AndAlso e.CellDescriptor.CellIndex > 2 Then e.Cell.Text = "Your value here" End If End Sub
Editing the DOM element
Another method of changing the cell value is to edit the DOM element under the PreRender event. By using the FindControl() method, the OlapGrid can be tracked in the table pattern. The particular cell position in the table can be detected by iterating the rows and columns of the table. The path of the header cells in the table is different from other cell types because of the expanders in the header cells.
The following code example illustrates how to set or change the cell value of the OlapGrid by using the QueryCellInfo event.
C#
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { } this.OlapClient1.OlapGrid.PreRender += OlapGrid_PreRender; } private void OlapGrid_PreRender(object sender, EventArgs e) { //The Header cells with expander and collapse symbol. (((Page.FindControl("OlapClient1").FindControl(this.OlapClient1.OlapGrid.ID).Controls[1] as Table).Rows[0].Cells[3].Controls[1]) as Literal).Text = "Some Text"; //The Header cells without expander and collapse symbol, applicable for value cells also. (((Page.FindControl("OlapClient1").FindControl(this.OlapClient1.OlapGrid.ID).Controls[1] as Table).Rows[0].Cells[3].Text) = "Some Text"; }
VB
Protected Sub Page_Load(sender As Object, e As EventArgs) If Not IsPostBack Then End If AddHandler Me.OlapClient1.OlapGrid.PreRender, AddressOf OlapGrid_PreRender End Sub Private Sub OlapGrid_PreRender(sender As Object, e As EventArgs) 'The Header cells with expander and collapse symbol. TryCast(TryCast(Page.FindControl("OlapClient1").FindControl(Me.OlapClient1.OlapGrid.ID).Controls(1), Table).Rows(0).Cells(3).Controls(1), Literal).Text = "Some Text" 'The Header cells without expander and collapse symbol, applicable for value cells also. TryCast(TryCast(Page.FindControl("OlapClient1").FindControl(Me.OlapClient1.OlapGrid.ID).Controls(1), Table).Rows(0).Cells(3).Text) = "Some Text" End Sub