DataGridView的验证和放大器;更改单元格值 [英] DataGridView Validation & Changing Cell Value

查看:248
本文介绍了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 =行[0] [批量]; 是不是做什么,我希望它做的。

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天全站免登陆