Click here to Skip to main content
16,004,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have loaded buttons in my grid view to update data, secondly I have a combobox that filters information in my grid view.
Now if I filtered information in my gridview and try to update filtered data in the grid, application throws error exception . That says “Input error not in a correct format”

Now why does it have to throw that error message only when updating filtered data from the gridview??:confused:


Below is the code when a button to edit is clicked in my gridview.

C#
#region Edit button handling for grvScheduledCourses in "View Scheduled Courses" Tab
        private void grvScheduledCourses_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
           
            int currentRow = int.Parse(e.RowIndex.ToString());
            try
            {
                string courseNoString = grvScheduledCourses[0, currentRow].Value.ToString();
                //****Format Error Exception occurs here****
                courseNo = int.Parse(courseNoString);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        
            if (grvScheduledCourses.Columns[e.ColumnIndex] == btnEdit && currentRow >= 0)
            {
                string courseCode = grvScheduledCourses[1, currentRow].Value.ToString();
                string StartDate = grvScheduledCourses[2, currentRow].Value.ToString();
               
                string name = grvScheduledCourses[6, currentRow].Value.ToString();
                string email = grvScheduledCourses[7, currentRow].Value.ToString();
                //runs frmAssignTrainer to edit trainers
                frmAssignTrainer f2 = new frmAssignTrainer();
                f2.courseCode = courseCode;
                f2.StartDate = StartDate;
                
                f2.name = name;
                f2.email = email;
                
                f2.courseNo = courseNo;
                f2.Show();
                grvScheduledCourses.Update();
                
            }
           
         }
        #endregion
Posted
Updated 5-Jan-11 1:18am
v3
Comments
nagendrathecoder 5-Jan-11 7:12am    
Question edited for formatting.

1 solution

Set a breakpoint at courseNo = int.Parse(courseNoString); and inspect courseNoString.

Why this: int currentRow = int.Parse(e.RowIndex.ToString());
e.RowIndex is an integer so you can do int currentRow = e.RowIndex


Use TryParse instead of parse to handle invalid input

Regards
Espen Harlinn
 
Share this answer
 
Comments
Anele Ngqandu 5-Jan-11 7:52am    
Hi, i tried to use the TryParse but it says "Cannot convert Bool to int"
Espen Harlinn 5-Jan-11 8:05am    
Have you tried setting a breakpoint? You can also use Log4Net to log courseNoString
Anele Ngqandu 5-Jan-11 8:13am    
yes i did use the breakpoint and the problem was in the courseNo variable, thats where it complains about the format.

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