C# 在数据网格视图中添加
: 我正在尝试获取数据,一旦用户键进入数据网格视图,它将自动将数据1和数据2添加到一起,并显示总数,但在这里它不起作用。这是我的代码。到目前为止,我已通过添加按钮使它们相加C# 在数据网格视图中添加,c#,graph,windows-forms-designer,C#,Graph,Windows Forms Designer,: 我正在尝试获取数据,一旦用户键进入数据网格视图,它将自动将数据1和数据2添加到一起,并显示总数,但在这里它不起作用。这是我的代码。到目前为止,我已通过添加按钮使它们相加 private void dataGridView1_CellStyleContentChanged(object sender, DataGridViewCellStyleContentChangedEventArgs e) { try { var
private void dataGridView1_CellStyleContentChanged(object sender, DataGridViewCellStyleContentChangedEventArgs e)
{
try
{
var a = Convert.ToDouble(dataGridView1.Rows[1].Cells[0].Value) + Convert.ToDouble(dataGridView1.Rows[2].Cells[0].Value);
dataGridView1.Rows[3].Cells[0].Value = a;
var b = Convert.ToDouble(dataGridView1.Rows[1].Cells[1].Value) + Convert.ToDouble(dataGridView1.Rows[2].Cells[1].Value);
dataGridView1.Rows[3].Cells[1].Value = b;
var c = Convert.ToDouble(dataGridView1.Rows[1].Cells[2].Value) + Convert.ToDouble(dataGridView1.Rows[2].Cells[2].Value);
dataGridView1.Rows[3].Cells[2].Value = c;
var d = Convert.ToDouble(dataGridView1.Rows[1].Cells[3].Value) + Convert.ToDouble(dataGridView1.Rows[2].Cells[3].Value);
dataGridView1.Rows[3].Cells[3].Value = d;
var f = Convert.ToDouble(dataGridView1.Rows[1].Cells[4].Value) + Convert.ToDouble(dataGridView1.Rows[2].Cells[4].Value);
dataGridView1.Rows[3].Cells[4].Value = f;
var g = Convert.ToDouble(dataGridView1.Rows[1].Cells[5].Value) + Convert.ToDouble(dataGridView1.Rows[2].Cells[5].Value);
dataGridView1.Rows[3].Cells[5].Value = g;
var h = Convert.ToDouble(dataGridView1.Rows[1].Cells[6].Value) + Convert.ToDouble(dataGridView1.Rows[2].Cells[6].Value);
dataGridView1.Rows[3].Cells[6].Value = h;
var i = Convert.ToDouble(dataGridView1.Rows[1].Cells[7].Value) + Convert.ToDouble(dataGridView1.Rows[2].Cells[7].Value);
dataGridView1.Rows[3].Cells[7].Value = i;
var j = Convert.ToDouble(dataGridView1.Rows[1].Cells[8].Value) + Convert.ToDouble(dataGridView1.Rows[2].Cells[8].Value);
dataGridView1.Rows[3].Cells[8].Value = j;
var k = Convert.ToDouble(dataGridView1.Rows[1].Cells[9].Value) + Convert.ToDouble(dataGridView1.Rows[2].Cells[9].Value);
dataGridView1.Rows[3].Cells[9].Value = k;
var l = Convert.ToDouble(dataGridView1.Rows[1].Cells[10].Value) + Convert.ToDouble(dataGridView1.Rows[2].Cells[10].Value);
dataGridView1.Rows[3].Cells[10].Value = l;
var n = Convert.ToDouble(dataGridView1.Rows[1].Cells[11].Value) + Convert.ToDouble(dataGridView1.Rows[2].Cells[11].Value);
dataGridView1.Rows[3].Cells[11].Value = n;
}
catch
{
MessageBox.Show("Never run");
}
}
您可以通过在dgvHmm中的所有行上循环来缩短整个代码,但我想要的是,当数据1和数据2的值为关键值时,它将自动计算总计并在总计列中显示总计值