0

私は問題があります。これが私の問題です:

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,0001001,000,000

DataGridView にデータを追加し、データが DataGridView に追加されたら、その DataGridView で編集をクリックします。値を変更すると、変更されて更新されます。しかし、問題は、「数量」を から に変更しようとしたときに、 を変更100500なかったことです。に基づいて更新および変更することを想定してTotalいます( を変更したため)TotalQuantity * Sub TotalQuantity

それはどうですか?

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)

ここに画像の説明を入力

すでに を に変更したときのスクリーンショットが表示されていますQuantity500Totalそれでも と同じです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同じままです。

4

1 に答える 1