私は問題があります。これが私の問題です:
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
上記は正しいです10,000
。100
1,000,000
DataGridView にデータを追加し、データが DataGridView に追加されたら、その DataGridView で編集をクリックします。値を変更すると、変更されて更新されます。しかし、問題は、「数量」を から に変更しようとしたときに、 を変更100
し500
なかったことです。に基づいて更新および変更することを想定してTotal
います( を変更したため)Total
Quantity * Sub Total
Quantity
それはどうですか?
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];
}
これは、数量を 100 から 500 に変更した前後の上記の問題のスクリーンショットです。
Quantity
を変更していないときのスクリーンショット500
(まだ 100)
すでに を に変更したときのスクリーンショットが表示されていますQuantity
が500
、Total
それでも と同じですQuantity 100
Quantity
基本的に、ユーザーが DataGridView で編集をクリックして変更を加えたときに必要です (ユーザーがDataGridViewで変更を加えたとしましょう。DataGridViewTotal
も変更する必要があります) 。
編集済み:
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();
}
}
注:
Quantity
値を変更しようとしても、編集されたコードは機能しませんが、Total
同じままです。