【发布时间】:2018-08-27 12:19:36
【问题描述】:
我有两个datagridview,每个都包含两列和相同的行数。我使用这个循环比较了这两个datagridview:
int result = 0;
for (int i=0;i< dgvInvent1.RowCount;i++)
{
var src1 = dgvInvent1.Rows[i].Cells[1].Value.ToString();
var src2 = dgvInvent2.Rows[i].Cells[1].Value.ToString();
result = Int32.Parse(src1) - Int32.Parse(src2);
}
我也想将结果转移到另一个datagridview,我将它命名为“dgvFinal”,除了dvgFinal是我刚刚创建的另一个名为“Form2”的表单,所以我将此行添加到form2
public DataGridView dvgFinal { get; set; }
并在我的主表单中添加到我的循环中
Form2 re = new Form2();
int result = 0;
for (int i=0;i< dgvInvent1.RowCount;i++)
{
var src1 = dgvInvent1.Rows[i].Cells[1].Value.ToString();
var src2 = dgvInvent2.Rows[i].Cells[1].Value.ToString();
result = Int32.Parse(src1) - Int32.Parse(src2);
re.dvgFinal.Rows[i].Cells[2].Value = result;
}
但它不起作用,我明白了
System.NullReferenceException was unhandled
HResult=-2147467261
Message=Object reference not set to an instance of an object.
有人可以帮助我吗? 提前谢谢你
【问题讨论】:
-
您有 2 个具有相同列和行的 dgv,您必须从
dgvInvent1减去第 1 列到dgvInvent2并在另一个来自的第三个 dgv 中显示结果,对吧? -
对对对
-
在您执行此循环时,“dvgFinal”的行数(至少三列)是否与“dgvInvent1”一样多?
-
不,'dvgFinal' 不包含任何数据,只是我预定义的列
标签: c# .net winforms datagridview