用户代码未处理invalidCastException [英] invalidCastException was unhandled by user code

查看:103
本文介绍了用户代码未处理invalidCastException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hai;


我使用C#工作Windows应用程序
我的问题是未处理InvalidCastException"
无法将对象从DBNull强制转换为其他类型."

在我的应用程序中,我有一个datagridview控件和一个文本框.
并且我创建了一个名为Mark的表,该表中有4个字段.它们是名称,标记,mark1,总计.

1)首先,我将数据绑定到datagridview中.我的代码是波纹管


hai;


I work windows application using C#
my problem is "InvalidCastException was unhandled"
"Object cannot be cast from DBNull to other types."

in my application ,i have a datagridview control and a textbox.
and also i create a table named as Mark, in this table have 4 fields. they are name,mark,mark1,total.

1)Firstly i bind the data in the datagridview. my code is bellow


private void Form4_Load(object sender, EventArgs e)
       {
           SqlDataAdapter da = new SqlDataAdapter("select * from  addr", con);
           DataSet ds = new DataSet();
           da.Fill(ds);
           dataGridView1.DataSource = ds.Tables[0];
       }


2)我确实添加了mark和mark1并存储在datagridview的total列中.我的代码如下所示



2) i do add mark and mark1 and store in total column in datagridview. my code is given bellow


private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
       {
           c1 = Convert.ToDouble(dataGridView1.CurrentRow.Cells[3].Value);
           a = Convert.ToDouble(dataGridView1.CurrentRow.Cells[1].Value);
           b = Convert.ToDouble(dataGridView1.CurrentRow.Cells[2].Value);
           dataGridView1.CurrentRow.Cells[3].Value = a * b;
           dataGridView1.Columns["Amount"].ReadOnly = true;

           c = 0;
           c = Convert.ToDouble(dataGridView1.CurrentRow.Cells[3].Value) - c1;
           ng = ng + Convert.ToDouble(c);
           c = 0;
           a = 0;
           textBox1.Text = ng.ToString();

       }



在此行发生错误"c1 = Convert.ToDouble(dataGridView1.CurrentRow.Cells [3] .Value);"//InvalidCastException未处理,这是错误消息.

如何解决这些问题以及如何在此datagrideview中编辑/添加新值.


请帮助我



the error occured at this line "c1 = Convert.ToDouble(dataGridView1.CurrentRow.Cells[3].Value);"//InvalidCastException was unhandled " this is the error message.

how to over come these problem and how to edit/ add a new value inthis datagrideview.


please help me

thanks in advance.

推荐答案

您可以使用Double.TryParse.这样您可以检查是否存在问题:

You can use the Double.TryParse. This way you can check if there is a problem:

Double x;
if (Double.TryParse(dataGridView1.CurrentRow.Cells[3].Value, out x)
  c1 = x;
else
{
  //Whatever the fallback will be.
}


这仅表示Cell[3]内容的类型不是double.只是检查一下.如果是字符串,则可以使用double.Parse(string)double.TryParse(string)进一步解析是否要加倍,但这将是不好的代码设计.您最好在正确的单元格中具有正确的类型,并最终在数据库的列中具有正确的类型.

顺便说一句,您正在使用硬编码的立即常量作为单元格索引;这总是很糟糕,难以维护,容易出错.

—SA
It simply means that the type of that Cell[3] content is not double. Just check it up. If it''s the string, you can further parse if to double using double.Parse(string) or double.TryParse(string), but it would be a bad code design. You should better have right types in right cells, and, ultimately, right types in the columns of your database.

By the way, you are using hard coded immediate constants for the cell indices; this is always bad, hard to maintain, error-prone.

—SA


这篇关于用户代码未处理invalidCastException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆