Click here to Skip to main content
15,915,319 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
private void dgv_Add_job_card_DoubleClick(object sender, EventArgs e)
       {
           txt_Items_No.Text = this.dgv_Add_job_card.CurrentRow.Cells[0].Value.ToString();
           txt_Job_card.Text = this.dgv_Add_job_card.CurrentRow.Cells[1].Value.ToString();
           txt_Value_Part.Text = this.dgv_Add_job_card.CurrentRow.Cells[2].Value.ToString();
           CB_Auto_parts.Text = this.dgv_Add_job_card.CurrentRow.Cells[3].Value.ToString();
           if (Convert.ToInt32(this.dgv_Add_job_card.CurrentRow.Cells[4].Value) == 0)
           {

               radioButton_New.Checked = true;

           }
           else
           {

               radioButton_old.Checked = true;
           }
           CB_Providers_desc.Text = this.dgv_Add_job_card.CurrentRow.Cells[5].Value.ToString();
           CB_Hand_installation.Text = this.dgv_Add_job_card.CurrentRow.Cells[6].Value.ToString();
           txt_Notes.Text = this.dgv_Add_job_card.CurrentRow.Cells[7].Value.ToString();
           CB_Repair_type.Text = this.dgv_Add_job_card.CurrentRow.Cells[10].Value.ToString();
           dgv_Add_job_card.Rows.RemoveAt(dgv_Add_job_card.CurrentRow.Index);
           txt_Value_Part.Focus();
       }
Posted

Indeed, it can't.

I suspect you're getting that due to:
Convert.ToInt32(this.dgv_Add_job_card.CurrentRow.Cells[4].Value)

First off, do NOT use Convert.ToInt32 .

Next, try this instead (if the value is an integer):
C#
if ((this.dgv_Add_job_card.CurrentRow.Cells[4].Value is int) && ((int)this.dgv_Add_job_card.CurrentRow.Cells[4].Value == 0))


Or this (if the value is a bit/boolean):
C#
if ((this.dgv_Add_job_card.CurrentRow.Cells[4].Value is bool) && (bool)this.dgv_Add_job_card.CurrentRow.Cells[4].Value))



Furthermore, if the other values are strings, do NOT use ToString on them; just cast them.
 
Share this answer
 
v3
Here:
C#
if (Convert.ToInt32(this.dgv_Add_job_card.CurrentRow.Cells[4].Value) == 0)

..that cell holds a DBNull-value (the single instance of DBNull to be exact) which probably originates from reading from your database. It can't be converted or cast to anything (other than Object). You have to check for it before attempting to check for being equal to 0. Probably like this:
C#
if (this.dgv_Add_job_card.CurrentRow.Cells[4].Value == DBNull.Value || (int)this.dgv_Add_job_card.CurrentRow.Cells[4].Value == 0)

("Probably" because I don't know your intention here.)

Replace the cast (int) by the actual type of that column if it's not int. You know the column types, no need to use Convert.ToXXX if you can just cast it.

If you don't want it to "be able to be DBNull" but just 0 instead you have to change your database-table-column to be non-nullable and when inserting into that table, insert 0 instead of null.
 
Share this answer
 
v2
Comments
PIEBALDconsult 30-Jan-16 15:07pm    
"you have to change your database-table-column"
Or use ISNULL (or similar) in the SELECT statement.
Sascha Lefèvre 30-Jan-16 15:12pm    
Right, that's also an option.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900