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);
In many, many cases the data is not actually in the DataGridView
, but elsewhere like a DataTable
or a collection of some sort (like List<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
.
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 multiple Quantity * Price
for some rows:
dtSample = new DataTable();
dtSample.Columns.Add(new DataColumn("Item", typeof(string)));
dtSample.Columns.Add(new DataColumn("Quantity", typeof(int)));
dtSample.Columns.Add(new DataColumn("Price", typeof(decimal)));
dtSample.Columns.Add(new DataColumn("Sale", typeof(decimal)));
// assign expression using the col names
dtSample.Columns[3].Expression = "(Quantity * Price)";
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 a Quantity
or Price
cell, the Sale
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 a DataSource
. Adding calculated rows could accidentally add new data to that source (like a DB). But it is not hard to do in code:
Similar to the DGV CellFormatting
answer given, you can respond to events from the DataTable
such as RowChanged
. There you can perform whatever operations and update the table.
...create table and columns
...populate table
// hook up event
dtSample.RowChanged += RowChanged;
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 the DataTable
. Unlike an Expression
, 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:
private void RowChanged(object sender, DataRowChangeEventArgs e)
{
// number of rows used
int Rows = dtSample.Rows.Count-1;
if (e.Row == dtSample.Rows[Rows]) return;
// display TotalSales / TotalUnits
// get the units
int TotUnits = dtSample
.AsEnumerable()
.Where(r => !r.IsNull("Quantity"))
.Take(Rows)
.Sum(n => n.Field<int>("Quantity"));
// sum Sales, divide and display in DGV
dtSample.Rows[Rows]["Price"] = dtSample
.AsEnumerable()
.Where(r => !r.IsNull("Sale"))
.Take(Rows)
.Sum(n => n.Field<decimal>("Sale")) / TotUnits;
}
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 of DataGridView
and put calculation logic there. Also handle CellEndEdit
and call InvalidateCell
or Invalidate
to force update value of cell after each in reference cells.
Here is an example:
void Form1_Load(object sender, EventArgs e)
{
Random r = new Random();
var dt = new DataTable();
dt.Columns.Add("A", typeof(int));
dt.Columns.Add("B", typeof(int));
for (int i = 0; i < 10; i++)
dt.Rows.Add(r.Next(100));
grid.DataSource = dt;
grid.CellFormatting += grid_CellFormatting;
grid.CellEndEdit += grid_CellEndEdit;
}
void grid_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
grid.Invalidate();
}
void grid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
var grid = sender as DataGridView;
var parameterColumnName = "A"; //Parameter column name
var start = 0; //Start row index for formula
var end = grid.RowCount - 1; //End row index for formula
var resultRowIndex = 0; //Result row index
var resultColumnName = "B"; //Result column name
if (e.RowIndex == resultRowIndex &&
grid.Columns[e.ColumnIndex].Name == resultColumnName)
{
var list = Enumerable.Range(start, end - start + 1)
.Select(i => grid.Rows[i].Cells[parameterColumnName].Value)
.Where(x => x != null && x != DBNull.Value)
.Cast<int>();
if (list.Any())
e.Value = list.Max();
}
}
Note
The solution is not limited to DataTable
, it will work regardless of the DataSource
which you use for DataGridView
and you can use any kind of data source in this solution.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With