I want to add formula cell in a DataGridView
. Is there any custom DataGridView
to do this?
Example:
grid[4, column].Text = string.Format("=MAX({0}6:{0}{1})", columnAsString, grid.RowCount);
I want to add formula cell in a DataGridView
. Is there any custom DataGridView
to do this?
Example:
grid[4, column].Text = string.Format("=MAX({0}6:{0}{1})", columnAsString, grid.RowCount);
A couple of things you could try:
Handle the cell value changed event to detect when one of the cells used in the calculation changes and calculate the appropriate value. Something like this (try it in a new project)
Or rather than reading your file direct to the datagridview read it into a datatable and bind the grid to the datatable. You can then add an expression column in the datatable to do the calculation. Something like this:
No, you are dealing with pure data strings, you need to have a thread running in the background to do the calculations and update the UI accordingly.
In many, many cases the data is not actually in the
DataGridView
, but elsewhere like aDataTable
or a collection of some sort (likeList<T>
). The control simply presents a view of the data to the user.There are several ways to do something along the lines of what you want. For both of these, the data actually resides in a
DataTable
.Expressions
A
DataColumn
can be assigned an Expression. Consult the link for the types of Expression, keywords, operators and functions supported. The following will create an expression based columns to multipleQuantity * Price
for some rows:After some random data is added, as well as an empty row, the
DataTable
will maintain those columns for you. This works like you probably want it to: if the user (or code) changes the value of aQuantity
orPrice
cell, theSale
column contents is automatically updated. (an image is later after the second method).Expressions
work at the row level. There is not an all-rows/table-wise counter part for something like a TOTALS row - this is because the data would often come from aDataSource
. Adding calculated rows could accidentally add new data to that source (like a DB). But it is not hard to do in code:Event Driven Calculations
Similar to the DGV
CellFormatting
answer given, you can respond to events from theDataTable
such asRowChanged
. There you can perform whatever operations and update the table.Then in the event, the code calculates an over all per unit average to display in the last row. In some cases, you may be able to use the
Compute()
method of theDataTable
. Unlike anExpression
, it isnt updated automatically and as shown in this answer it can be clumsy to update.With typed data in a DataTable, it is fairly easy to perform calculations in response to events:
The "Sales" column is automatically maintained via an
Expression
which means you cannot manually do anything to that column.The overall average price at the bottom is also "automatically" updated, the difference is that we had to write a smattering of code to do so.
Is there any custom DataGridView to do this?
Off-topic, but if you are looking for a custom control, take a look at Free .NET Spreadsheet Control. Also it supports formula.
If writing code for calculation is an option for you
If writing code for calculation is an option for you, to calculate value of a cell based on values of some other cells you can use
CellFormatting
event ofDataGridView
and put calculation logic there. Also handleCellEndEdit
and callInvalidateCell
orInvalidate
to force update value of cell after each in reference cells.Here is an example:
Note
The solution is not limited to
DataTable
, it will work regardless of theDataSource
which you use forDataGridView
and you can use any kind of data source in this solution.