Reputation: 2883
i have a problem. Here is my problem:
I got something like this in my DataGridView:
Product Code || Quantity || Description || SubTotal || Total || ....... (and so on)
SM0001 for the Product Code,
100 for the Quantity,
AS 5 for the Description,
10,000 for the Sub Total, and 1,000,000 for the Total
The above it's correct because 10,000
times 100
we get 1,000,000
I add data to the DataGridView and when the data has been added to the DataGridView, i click edit in that DataGridView, when i change some value on it, it will be changed and updated. But, the problem is when i tried to change the "Quantity" from 100
to 500
, it didn't change the Total
, the Total
suppose to update and change too based on Quantity * Sub Total
(because i change the Quantity
)
How is it like that?
Here is the code for my problem above when i tried to update it from DataGridView:
private void DataGridViewCalculation(object sender, EventArgs e)
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
int _quantity = Convert.ToInt32(this.dataGridView1.Rows[i].Cells[1].Value);
int _subTotal = Convert.ToInt32(this.dataGridView1.Rows[i].Cells[3].Value);
int _total = Convert.ToInt32(_quantity * _subTotal);
this.dataGridView1.Rows[i].Cells[4].Value = _total;
}
dataGridView1.DataSource = null;
dataGridView1.DataSource = _ds.Tables[0];
}
Here is the screenshot of my problem above after and before i change the quantity to 500 from 100:
Screenshot above display when i haven't change the Quantity
to 500
(still at 100)
Screenshot above display when i already changed the Quantity
to 500
, but the Total
still the same as Quantity 100
Basically, i want when user click EDIT in DataGridView and made changes in it (let's say user make changes on Quantity
in DataGridView, the Total
in DataGridView should change too)
EDITED:
dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(this.DataGridViewCalculation);
private void DataGridViewCalculation(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex.Equals(1))
{
int _total = Convert.ToInt32(dataGridView1["Quantity", e.RowIndex].Value) * Convert.ToInt32(dataGridView1["SubTotal", e.RowIndex].Value);
dataGridView1["Total", e.RowIndex].Value = _total.ToString();
}
}
NOTE:
The edited code still not working when i tried to change the Quantity
value, the Total
still remains same.
Upvotes: 0
Views: 2328
Reputation: 8902
You can use CellValueChanged
event. This event will be raised when cell value is changed.
Basically what you have to do is,
Sub Total
column and Quantity
in the same row using RowIndex
of the edited cell.Total
cell.Sample Code
Please note that this is a sample code, You may have to consider converting values to the required numeric format before performing any operation.
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex.Equals("column Index of Quantity"))
{
double total = Convert.ToDouble(dataGridView1["Subtotal column name", e.RowIndex].Value) * Convert.ToDouble(dataGridView1["Quantity column name", e.RowIndex].Value);
dataGridView1["Total Column name", e.RowIndex].Value = total.ToString();
}
}
Upvotes: 2