我有 4 个复选框。同时,一次检查 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);
}
}
}