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) 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();
}
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.