DataGridView 验证和更改单元格值

发布于 10-13 02:25 字数 1633 浏览 8 评论 0原文

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

我能够正确验证我的值,但是当我尝试将其更改为有效的值时,我收到数据错误。这是我的代码:

        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"]; 的行没有执行我期望的操作。

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.

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;
                }
            }
        }
    }

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戏舞2024-10-20 02:25:51

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

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

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

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

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.

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.

潇烟暮雨2024-10-20 02:25:51

一般来说,最好使用 CellParsing 事件每当您需要转换/更改单元格中的值时。在该事件中,您可以通过设置 ErrorText 单元格或行的值。

In general, it is better to use the CellParsing event whenever you need to convert/change the value in a cell. From within that event, you can indicate that the user's value is invalid by setting the ErrorText value of the cell or row.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文