Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C#
private void btn_Add_Job_card_Click(object sender, EventArgs e)
       {

           Validation_Add_Job_card_order();
           if (validate == true)
           {
               DataRow r = dt.NewRow();
               // = dt.Rows.Count + 1;
               r[0] = dt.Rows.Count + 1;
               //r[0] = txt_Items_No.Text;
               r[1] = txt_Job_card.Text;
               r[2] = txt_Value_Part.Text;
               r[3] = CB_Auto_parts.Text;
               r[4] = x;
               r[5] = CB_Providers_desc.Text;
               r[6] = CB_Hand_installation.Text;
               r[7] = txt_Notes.Text;

               dt.Rows.Add(r);
               dgv_Add_job_card.DataSource = dt;
               ClearText();

               txt_Total_Values.Text = (from DataGridViewRow row in dgv_Add_job_card.Rows
                                        where row.Cells[2].FormattedValue.ToString() != string.Empty
                                        select Convert.ToDouble(row.Cells[2].FormattedValue)).Sum().ToString();
           }
       }

private void btn_Add_Click(object sender, EventArgs e)
        {
 for (int i = 0; i < dgv_Add_job_card.Rows.Count - 1; i++)
                    {
                        clsParts.ADD_AUTO_PART_DETAILS(Convert.ToInt32(dgv_Add_job_card.Rows[i].Cells[0].Value)
                                                      ,dgv_Add_job_card.Rows[i].Cells[1].Value.ToString()
                                                      ,dgv_Add_job_card.Rows[i].Cells[2].Value.ToString()
                                                      ,Convert.ToInt32(dgv_Add_job_card.Rows[i].Cells[3].Value)
                                                      ,Convert.ToBoolean(dgv_Add_job_card.Rows[i].Cells[4].Value)
                                                      ,Convert.ToInt32(dgv_Add_job_card.Rows[i].Cells[5].Value)
                                                      ,Convert.ToInt32(dgv_Add_job_card.Rows[i].Cells[6].Value).
                                                      ,dgv_Add_job_card.Rows[i].Cells[7].Value.ToString());
                    }
}
Posted
Comments
Kevin Marois 18-Jan-16 14:17pm    
That's nice. Now do you have a question?
Member 12244977 18-Jan-16 14:20pm    
this error , input string was not in a correct format in C# windows form....
int this part:-

for (int i = 0; i < dgv_Add_job_card.Rows.Count - 1; i++)
{
clsParts.ADD_AUTO_PART_DETAILS(Convert.ToInt32(dgv_Add_job_card.Rows[i].Cells[0].Value)
,dgv_Add_job_card.Rows[i].Cells[1].Value.ToString()
,dgv_Add_job_card.Rows[i].Cells[2].Value.ToString()
,Convert.ToInt32(dgv_Add_job_card.Rows[i].Cells[3].Value)
,Convert.ToBoolean(dgv_Add_job_card.Rows[i].Cells[4].Value)
,Convert.ToInt32(dgv_Add_job_card.Rows[i].Cells[5].Value)
,Convert.ToInt32(dgv_Add_job_card.Rows[i].Cells[6].Value).
,dgv_Add_job_card.Rows[i].Cells[7].Value.ToString());
}
}
ZurdoDev 18-Jan-16 14:19pm    
This is a very easy thing for you to fix on your own.

1. For us to help you, you have to tell us which line of code caused the error, otherwise we are just guessing.
2. If you debug the code you'll see what the value is and you'll see exactly why there is a problem.
Member 12244977 18-Jan-16 14:20pm    
this error , input string was not in a correct format in C# windows form....
int this part:-

for (int i = 0; i < dgv_Add_job_card.Rows.Count - 1; i++)
{
clsParts.ADD_AUTO_PART_DETAILS(Convert.ToInt32(dgv_Add_job_card.Rows[i].Cells[0].Value)
,dgv_Add_job_card.Rows[i].Cells[1].Value.ToString()
,dgv_Add_job_card.Rows[i].Cells[2].Value.ToString()
,Convert.ToInt32(dgv_Add_job_card.Rows[i].Cells[3].Value)
,Convert.ToBoolean(dgv_Add_job_card.Rows[i].Cells[4].Value)
,Convert.ToInt32(dgv_Add_job_card.Rows[i].Cells[5].Value)
,Convert.ToInt32(dgv_Add_job_card.Rows[i].Cells[6].Value).
,dgv_Add_job_card.Rows[i].Cells[7].Value.ToString());
}
}
ZurdoDev 18-Jan-16 14:32pm    
Probably dgv_Add_job_card.Rows[i].Cells[0].Value is not an integer. But we can't see your code. You have to run it and see if that is the actual problem.

We can't run your code and say "it's that bit" - because we can;t run your code in isolation and get the same results you do - we don't have access to your DGV or top the data you are filling it with.

We'd have to guess what the problem might be, where for you a simple use of the debugger will tell you exactly what the problem is.
So use the debugger. Run your app under the debugger (F5 in VS) and make the problem happen.
When the error occurs, look at the parts of the line of your code it is complaining about, and see what value does not match the data the DGV is expecting. Then you can look back in your code to work out why - we can't!

If I was to guess, I'd say it's probably that dgv_Add_job_card.Rows[i].Cells[0].Value isn't an integer or the string representation of an integer - but what value it contains and where the heck that value comes from only you can tell! :laugh:
 
Share this answer
 
Comments
Member 12244977 18-Jan-16 14:41pm    
ALTER proc [dbo].[ADD_AUTO_PART_DETAILS]
@item_no int,
@job_card varchar(50),
@value_part varchar(50),
@auto_parts_id int,
@new_old_part bit,
@provider_id int,
@installation_id int,
@notes text
as
insert into Auto_Part_Details
([Items_No]
,[Job_Card]
,[Value_Part]
,[Auto_Parts_id]
,[New_Old_Part]
,[Provider_id]
,[Installation_id]
,[Notes])
values (@item_no
,@job_card
,@value_part
,@auto_parts_id
,@new_old_part
,@provider_id
,@installation_id
,@notes)

this is the insert query :)
Member 12244977 18-Jan-16 14:50pm    
oh, i want tell u something when i insert one row only this code is work but when i insert more than rows this error happened
You call a number of Convert.To* methods having no guarantee that the string passed as an argument really can be parsed as a value of required type. If it doesn't, you throw an exception, which you can handle and recover. But this is not a right way. Even the class name Convert is somewhat misleading. You need to understand what really happens, and this is nothing but parsing. You should better use the TryParse method for each expected type. For example, for int, you can use bool int.TryParse(string, out int), and so on. If such method returns false, you know that the input string is not in an expected format, and the valid value is not returned via the second out parameter.

Moreover, your code sample strongly suggests that you tend to work with strings representing data, instead of data itself. You should better avoid it, reserving strings mostly for output purposes, such as showing on screen. For example, for the type decimal and integer types, you can use the control System.Windows.Forms.NumericUpDown (in can also be used indirectly for floating-point type, if you need input with fixed precision, which is usually the case): NumericUpDown Class (System.Windows.Forms)[^].

Also, you can use System.Windows.Forms.DataGridView where you can use strongly-typed cell data. Some types of the cell derived from System.Windows.Forms.DataGridViewCell are already available for some commonly used data types, some you can develop yourself. Please see:
DataGridView Class (System.Windows.Forms)[^],
DataGridViewCell Class (System.Windows.Forms)[^],
DataGridViewCell.Value Property (System.Windows.Forms)[^],
DataGridViewCell.ValueType Property (System.Windows.Forms)[^].

And so on… It were just some of the idea, something to think about.

—SA
 
Share this answer
 

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