我想添加第1行和第2行并将其显示在第3行但我面临一些错误可以有人请帮助谢谢
The Error at the moment
private void button2_Click(object sender, EventArgs e) /// Click button it will do the adding
{
String a; // total value for row 3 cell 0
dataGridView1.Rows[1].Cells[0].Value.ToString();
dataGridView1.Rows[2].Cells[0].Value.ToString();
Convert.ToDouble(dataGridView1.Rows[1].Cells[0].Value);
Convert.ToDouble(dataGridView1.Rows[2].Cells[0].Value);
a = dataGridView1.Rows[1].Cells[0].Value + dataGridView1.Rows[1].Cells[0].Value; /// error
dataGridView1.Rows[3].Cells[0].Value = a;
String b;// total value for row 3 cell 1
dataGridView1.Rows[1].Cells[1].Value.ToString();
dataGridView1.Rows[2].Cells[1].Value.ToString();
b = dataGridView1.Rows[1].Cells[1].Value.ToString() + dataGridView1.Rows[1].Cells[1].Value.ToString();
dataGridView1.Rows[3].Cells[1].Value = b;
String c;// total value for row 3 cell 2
dataGridView1.Rows[1].Cells[2].Value.ToString();
dataGridView1.Rows[2].Cells[2].Value.ToString();
c = dataGridView1.Rows[1].Cells[2].Value.ToString() + dataGridView1.Rows[1].Cells[2].Value.ToString();
dataGridView1.Rows[3].Cells[2].Value = c;
}
答案 0 :(得分:0)
您正在添加两个对象类型,这将始终抛出错误,因为编译器不允许这样做。
或者,您可以使用:
var d = Convert.ToDouble(dataGridView1.Rows[1].Cells[0].Value) + Convert.ToDouble(dataGridView1.Rows[2].Cells[0].Value);
dataGridView1.Rows[3].Cells[0].Value = d;
使用此链接了解c#https://msdn.microsoft.com/en-us/library/system.convert(v=vs.110).aspx
中的转换类