C# 如何解决';系统。无效卡斯特例外';在DataGridView C中计算时#
我试图在C#中的DataGridView中水平添加行,垂直添加列,但每次尝试输入数据时都会收到以下错误消息: “类型为'System.InvalidCastException'的未处理异常” 发生在mscorlib.dll中。其他信息:无法创建对象 从DBNull强制转换为其他类型” 这是我的密码:C# 如何解决';系统。无效卡斯特例外';在DataGridView C中计算时#,c#,sql-server,datagridview,C#,Sql Server,Datagridview,我试图在C#中的DataGridView中水平添加行,垂直添加列,但每次尝试输入数据时都会收到以下错误消息: “类型为'System.InvalidCastException'的未处理异常” 发生在mscorlib.dll中。其他信息:无法创建对象 从DBNull强制转换为其他类型” 这是我的密码: private void tbl_valuesDataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
private void tbl_valuesDataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
//totalling horizontally across rows
int val1 = Convert.ToInt32(tbl_valuesDataGridView.Rows[e.RowIndex].Cells[1].Value);
int val2 = Convert.ToInt32(tbl_valuesDataGridView.Rows[e.RowIndex].Cells[2].Value);
tbl_valuesDataGridView.Rows[e.RowIndex].Cells[3].Value = val1 + val2;
//adding vertically down the total column to get Grand Total
double sum = 0;
for (int i = 0; i < tbl_valuesDataGridView.Rows.Count; ++i)
{
sum += Convert.ToDouble(tbl_valuesDataGridView.Rows[i].Cells[2].Value);
}
textBox1.Text = sum.ToString();
}
private void tbl\u valuesDataGridView\u CellEndEdit(对象发送方,DataGridViewCellEventArgs e)
{
//跨行水平求和
int val1=Convert.ToInt32(tbl_valuesDataGridView.Rows[e.RowIndex].Cells[1].Value);
int val2=Convert.ToInt32(tbl_valuesDataGridView.Rows[e.RowIndex].Cells[2].Value);
tbl_valuesDataGridView.Rows[e.RowIndex]。单元格[3]。Value=val1+val2;
//垂直向下添加总计列以获得总计
双和=0;
对于(int i=0;i
注意事项:
有人能帮我一下吗?对于水平加法代码,您可以设置单元格[3]。值等于单元格[1]。值+单元格[2]。值。 这很好,但您似乎只做了一行 然后在垂直加法中,对单元格[2]求和。所有行的值 如果我正确理解了代码,您应该在此处合计单元格[3]。值(因为这是存储每行总计的位置)
在任何情况下,您都需要转到引发异常的行,并检查调试器监视窗口中的每个变量。这将告诉您哪个对象处于错误状态。在Anik的帮助和一些调试下,我能够得到问题的答案。对于我们这些刚接触编程的人来说,如果您打算使用DataGridView,您会发现这段代码非常有用。始终确保对此代码使用CellEndEdit事件处理程序
private void tbl_valuesDataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
//totalling horizontally across rows
int val1 = 0, val2 = 0, demo1;
if (int.TryParse(tbl_valuesDataGridView.Rows[e.RowIndex].Cells[1].Value.ToString().Trim(), out demo1))
val1 = Convert.ToInt32(tbl_valuesDataGridView.Rows[e.RowIndex].Cells[1].Value);
if (int.TryParse(tbl_valuesDataGridView.Rows[e.RowIndex].Cells[2].Value.ToString().Trim(), out demo1))
val2 = Convert.ToInt32(tbl_valuesDataGridView.Rows[e.RowIndex].Cells[2].Value);
tbl_valuesDataGridView.Rows[e.RowIndex].Cells[3].Value = val1 + val2;
//Adding vertically down the total column
double sum = 0;
for (int i = 0; i < tbl_valuesDataGridView.Rows.Count; ++i)
{
sum += Convert.ToDouble(tbl_valuesDataGridView.Rows[i].Cells[3].Value); //where 1 represents the index of the column you are adding
}
textBox1.Text = sum.ToString();
}
private void tbl\u valuesDataGridView\u CellEndEdit(对象发送方,DataGridViewCellEventArgs e)
{
//跨行水平求和
int val1=0,val2=0,demo1;
if(int.TryParse(tbl_valuesDataGridView.Rows[e.RowIndex].Cells[1].Value.ToString().Trim(),out demo1))
val1=Convert.ToInt32(tbl_值DataGridView.Rows[e.RowIndex].Cells[1].Value);
if(int.TryParse(tbl_valuesDataGridView.Rows[e.RowIndex].Cells[2].Value.ToString().Trim(),out demo1))
val2=Convert.ToInt32(tbl_valuesDataGridView.Rows[e.RowIndex].Cells[2].Value);
tbl_valuesDataGridView.Rows[e.RowIndex]。单元格[3]。Value=val1+val2;
//垂直向下添加总计列
双和=0;
对于(int i=0;i
具体在哪一行?