DataGridView 验证 &更改单元格值 [英] DataGridView Validation & Changing Cell Value

查看:26
本文介绍了DataGridView 验证 &更改单元格值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在验证时操作 DataGridView 中的单元格,以便如果用户输入的值对数据库无效,但很容易转换为有效数据,程序会将值更改为适当的一个.

I would like to manipulate a cell in my DataGridView when it is validating so that if the user enters a value that is not valid for the database, but is easily converted to valid data, the program will change the value to an appropriate one.

我能够正确验证我的值,但是当我尝试将其更改为有效值时,我收到了 DataError.这是我的代码:

I am able to validate my value properly but when I try to change it to something valid I get a DataError. Here is my code:

        private void unit_List_2_GroupsDataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
    {
        Console.WriteLine("Validating");
        DataGridViewColumn col = this.unit_List_2_GroupsDataGridView.Columns[e.ColumnIndex];
        DataGridViewCell cell = this.unit_List_2_GroupsDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex];
        if (col == this.batchDataGridViewTextBoxColumn && this.unit_List_2_GroupsDataGridView.IsCurrentCellInEditMode)
        {
            Console.WriteLine("   Batch Column");
            DataRow[] rows = label_EntryDataSet.viewJobBatchList.Select(String.Format("Job={0} AND Display='{1}'"
                , comboBox1.SelectedValue, e.FormattedValue));
            if (rows.Length == 1)
            {
                Console.WriteLine("      Auto Completed item from list: {0}", rows[0]["Batch"]);
                //e.Cancel = true;
                cell.Value = rows[0]["Batch"];
                //this.unit_List_2_GroupsDataGridView.EndEdit();
            }
            else
            {
                Console.WriteLine("     No Autocomplete!");
                int i = 0;
                if (!int.TryParse(e.FormattedValue.ToString(), out i))
                {
                    Console.WriteLine("         Not an integer either");
                    e.Cancel = true;
                }
            }
        }
    }

读取 cell.Value = rows[0]["Batch"]; 的行没有按照我的预期执行.

The line that reads cell.Value = rows[0]["Batch"]; is not doing what I expect it to do.

推荐答案

CellValidating 事件发生在 DataGridView 离开编辑模式之前;这是一个与编辑控件 (DataGridView.EditingControl) 相关/涉及的事件.您永远不应该尝试更改此事件处理程序中的单元格值,因为除非您取消该事件(在这种情况下用户卡在编辑模式中),否则单元格值会在活动结束.因此,这会撤消您在处理程序中执行的任何操作.

The CellValidating event occurs just prior to when the DataGridView leaves edit mode; it's an event that relates-to/involves the editing control (DataGridView.EditingControl). You should never attempt to change the cell value in the handler for this event, because unless you cancel the event (in which case the user is stuck in edit mode), the cell value is set to the value from the editing control immediately after the event finishes. This, therefore, undoes any action you perform in the handler.

您需要做的是更改编辑控件中的值(记住不要取消事件).例如,对于 DataGridViewTextBoxCell,您可以使用以下内容代替有问题的行:

What you have to do instead is change the value in the editing control (remembering not to cancel the event). For example, for a DataGridViewTextBoxCell, you would use the following instead of your problematic line:

unit_List_2_GroupsDataGridView.EditingControl.Text = Convert.ToString(rows[0]["Batch"]);

您应该会发现这可以解决您的问题.

You should find that this solves your issue.

这篇关于DataGridView 验证 &更改单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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