DataGridView Calculated Column update when move to new row C#
-
I am using Visual Studio 2010 with Sql server Express Edition 2005 as backend DB. I've a form with DataGridView with following columns: OrderID ProductID UnitPrice Quantity Total DataGridView is bind to Dataset Table, "Total" is a Calculated column which I've added using following code:
column = new DataColumn();
column.DataType = Type.GetType("System.Double");
column.ColumnName = "Total";
ds.Tables[0].Columns.Add(column);
ds.Tables[0].Columns["Total"].Expression = "UnitPrice * Quantity";Code works fine, it calculate the Total by multiplying UnitPrice to Quantity. The problem is this calculation is made when I move to new row. What I need is to it calculate Total when there is any change in UnitPrice or Quantity. Is it possible? Any ideas/Suggestions will be highly appreciated. Regards Ahmed
-
I am using Visual Studio 2010 with Sql server Express Edition 2005 as backend DB. I've a form with DataGridView with following columns: OrderID ProductID UnitPrice Quantity Total DataGridView is bind to Dataset Table, "Total" is a Calculated column which I've added using following code:
column = new DataColumn();
column.DataType = Type.GetType("System.Double");
column.ColumnName = "Total";
ds.Tables[0].Columns.Add(column);
ds.Tables[0].Columns["Total"].Expression = "UnitPrice * Quantity";Code works fine, it calculate the Total by multiplying UnitPrice to Quantity. The problem is this calculation is made when I move to new row. What I need is to it calculate Total when there is any change in UnitPrice or Quantity. Is it possible? Any ideas/Suggestions will be highly appreciated. Regards Ahmed
-
after looking a little more, it may be better to use the cellvaluechanged event... sample (note this was tested only in C# win32.forms, and the column ValueTypes are strings):
private void dataGrid_CellValueChanged(object sender, DataGridViewCellEventArgs e) {
/*check Changing cell is "not" in the "Total" Column,
*would keep calling cell changed event, when the "Total" cell value changes*/
if ((e.RowIndex >= 0) && e.ColumnIndex != DataGrid.Columns["Total"].Index) {
//r = row of the changed Cell.
int r = e.RowIndex;//get values from cells in the same row int Qty = 0; int.TryParse((string)DataGrid\["Quantity", r\].Value, out Qty); double Price = 0D; double.TryParse((string)DataGrid\["UnitPrice", r\].Value, out Price); //Calculate Total, Change cell value in "Total" DataGrid\["Total", r\].Value = Price \* Qty; }
}
-
after looking a little more, it may be better to use the cellvaluechanged event... sample (note this was tested only in C# win32.forms, and the column ValueTypes are strings):
private void dataGrid_CellValueChanged(object sender, DataGridViewCellEventArgs e) {
/*check Changing cell is "not" in the "Total" Column,
*would keep calling cell changed event, when the "Total" cell value changes*/
if ((e.RowIndex >= 0) && e.ColumnIndex != DataGrid.Columns["Total"].Index) {
//r = row of the changed Cell.
int r = e.RowIndex;//get values from cells in the same row int Qty = 0; int.TryParse((string)DataGrid\["Quantity", r\].Value, out Qty); double Price = 0D; double.TryParse((string)DataGrid\["UnitPrice", r\].Value, out Price); //Calculate Total, Change cell value in "Total" DataGrid\["Total", r\].Value = Price \* Qty; }
}