We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How do I have formula cells in a GridDataBoundGrid?

Platform: WinForms |
Control: GridDataBoundGrid(Classic) |
Published Date: April 30, 2011 |
Last Revised Date: July 10, 2014

 

If you want to use formula cells in a GridDataBoundGrid, then you cannot directly use indexers like grid(row,col).CellType = "FormulaCell" to set style properties for an individual cell.

The reason is that in a GridDataBoundGrid, there is nothing stored in the grid on a cell by cell basis. The only cell specific information that is available is the value (which comes from the datasource and is not stored in the grid). So, if you want to use formulas in a GridDataBoundGrid, then you must provide the formula and also provide the FormulaTag, both on a cell basis. The attached sample uses the Model.QueryCellInfo event handler to provide the formula (generated in the handler dynamically), and to provide the FormulaTag (saved in an arraylist). It also handles the Model.SaveCellInfo event to make sure the FormulaTag information is updated properly.

C#

//Maintain formulaTags for the cells

formulaTags = new ArrayList();

for(int i = 0; i <= dt.Rows.Count + 1; ++i)

{

formulaTags.Add(null);

}

private void GridSaveCellInfo(object sender, GridSaveCellInfoEventArgs e)

{

if(e.ColIndex == 3 && e.RowIndex > 0 && e.RowIndex <= dt.Rows.Count)

{

formulaTags[e.RowIndex - 1] = e.Style.FormulaTag;

e.Handled = true;

}

}

private void GridQueryCellInfo(object sender, GridQueryCellInfoEventArgs e)

{

if(e.ColIndex == 3 && e.RowIndex > 0 && e.RowIndex <= dt.Rows.Count)

{

e.Style.CellType = "FormulaCell";

e.Style.Text = string.Format("=({0}{1} + {2}{1}) / 2", GridRangeInfo.GetAlphaLabel(1), GridRangeInfo.GetNumericLabel(e.RowIndex),GridRangeInfo.GetAlphaLabel(2));

e.Style.FormulaTag = (GridFormulaTag) this.formulaTags[e.RowIndex - 1];

e.Handled = true;

}

}

VB

'Maintain formulaTags for the cells

formulaTags = New ArrayList()

i = 0

Do While i <= dt.Rows.Count + 1

formulaTags.Add(Nothing)

i += 1

Loop

Private Sub GridSaveCellInfo(ByVal sender As Object, ByVal e As GridSaveCellInfoEventArgs)

If e.ColIndex = 3 AndAlso e.RowIndex > 0 AndAlso e.RowIndex <= dt.Rows.Count Then

formulaTags(e.RowIndex - 1) = e.Style.FormulaTag

e.Handled = True

End If

End Sub

Private Sub GridQueryCellInfo(ByVal sender As Object, ByVal e As GridQueryCellInfoEventArgs)

If e.ColIndex = 3 AndAlso e.RowIndex > 0 AndAlso e.RowIndex <= dt.Rows.Count Then

e.Style.CellType = "FormulaCell"

e.Style.Text = String.Format("=({0}{1} + {2}{1}) / 2", GridRangeInfo.GetAlphaLabel(1), GridRangeInfo.GetNumericLabel(e.RowIndex),GridRangeInfo.GetAlphaLabel(2))

e.Style.FormulaTag = CType(Me.formulaTags(e.RowIndex - 1), GridFormulaTag)

e.Handled = True

End If

End Sub

Here is the link with both CS and VB samples: http://websamples.syncfusion.com/samples/kb/grid.windows/GDBGFormula/main.htm

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon