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
.
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 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:
Event Driven Calculations
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.