4 つのチェックボックスがあります。一度に 1 つずつチェックすると、対応するデータグリッド ビューが表示されます。各データグリッド ビューから合計を取得し、テキスト ボックスに保存したいと考えています。次に、すべてのデータグリッド ビューの合計を表示したいのですが、1 つのデータグリッド ビューからのみ値を取得しています。これを行うにはどうすればよいですか?
private void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e)
{
if (checkBox1.Checked == true)
{
if (e.ColumnIndex == 2 || e.ColumnIndex == 3)
{
int a = Convert.ToInt32(dataGridView1.CurrentRow.Cells[2].Value);
int b = Convert.ToInt32(dataGridView1.CurrentRow.Cells[3].Value);
dataGridView1.CurrentRow.Cells[4].Value = a * b;
// sum = 0;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
sum += Convert.ToInt32(dataGridView1.Rows[i].Cells[4].Value);
}
// textBox2.Text = sum.ToString();
}
}
else if(checkBox2.Checked==true)
{
if (e.ColumnIndex == 2 || e.ColumnIndex == 3)
{
int a = Convert.ToInt32(dataGridView1.CurrentRow.Cells[2].Value);
int b = Convert.ToInt32(dataGridView1.CurrentRow.Cells[3].Value);
dataGridView1.CurrentRow.Cells[4].Value = a * b;
// s = 0;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
s += Convert.ToInt32(dataGridView1.Rows[i].Cells[4].Value);
}
}
}
else if (checkBox3.Checked == true)
{
if (e.ColumnIndex == 2 || e.ColumnIndex == 3)
{
int a = Convert.ToInt32(dataGridView1.CurrentRow.Cells[2].Value);
int b = Convert.ToInt32(dataGridView1.CurrentRow.Cells[3].Value);
dataGridView1.CurrentRow.Cells[4].Value = a * b;
// u = 0;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
u += Convert.ToInt32(dataGridView1.Rows[i].Cells[4].Value);
}
}
}
else if(checkBox4.Checked==true)
{
if (e.ColumnIndex == 2 || e.ColumnIndex == 3)
{
int a = Convert.ToInt32(dataGridView1.CurrentRow.Cells[2].Value);
int b = Convert.ToInt32(dataGridView1.CurrentRow.Cells[3].Value);
dataGridView1.CurrentRow.Cells[4].Value = a * b;
// m = 0;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
m += Convert.ToInt32(dataGridView1.Rows[i].Cells[4].Value);
}
}
}