网格视图中的更新按钮 [英] Update Button in a gridview

查看:67
本文介绍了网格视图中的更新按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网格视图中加载了用于更新数据的按钮,其次,我有一个组合框可以过滤网格视图中的信息.
现在,如果我在gridview中过滤了信息并尝试更新网格中的过滤数据,则应用程序将引发error exception.表示输入错误的格式不正确"

现在为什么只在从gridview更新过滤的数据时才必须抛出该错误消息?:confused:


以下是在我的gridview中单击要编辑的按钮时的代码.

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.

#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

推荐答案

courseNo = int.Parse(courseNoString);处设置断点并检查courseNoString.

为什么这样做:int currentRow = int.Parse(e.RowIndex.ToString());
e.RowIndex是一个整数,因此您可以将int currentRow = e.RowIndex


使用TryParse而不是parse处理无效的输入

问候
Espen Harlinn
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


这篇关于网格视图中的更新按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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