处理的dataGridView FormatExeption上编辑 [英] Handle dataGridView FormatExeption on edit

查看:164
本文介绍了处理的dataGridView FormatExeption上编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个DataGridView与四列。最后一列的类型是日期时间以小时和分钟(HH:MM)。

I'm making a datagridview with four columns. The last column's type is DateTime in Hours and minutes (HH:mm).

    DataTable.Columns.Add("Time", typeof(DateTime)); //fourth column
    dataGridView2.Columns[3].DefaultCellStyle.Format = "HH:mm";

当我把一个有效的HH:MM(12:37)格式它工作得很好,但它给了我一个错误信息,如果格式是无效的。(12:374)

When i put in a valid HH:mm(12:37) format it works just fine, but it gives me an error message if the format is invalid (12:374).

    The string wasn't regigniced as a valid DateTime --> System.FormatExeption



它告诉我处理DataError-exeption / FormatExeption改变时,会发生什么发生错误,但我怎么做到这一点?

It tells me to handle the "DataError-exeption / FormatExeption" to change what happens when the error occurs, but how do i do that?

我想发生错误之前回到它的价值。

I want it to go back to the value it was before the error occurs.

任何帮助将是赞赏。先谢谢了。

Any help will be appreciated. thanks in advance.

PS。如果我是不清楚的地方,或者如果你需要更多的信息,那么就说明什么需要

PS. If i'm unclear somewhere, or if you need more information then just explain what's needed.

编辑:我是直接从DataGridView的编辑时间值

I am editing the time value directly from the dataGridView.

推荐答案

处理事件的 DataGridView.DataError ,这样就可以适当的管理情况,显示正确的信息给用户。

Handle the event DataGridView.DataError so that you can proper manage the situation and show the correct messages to the user.

下面的事件处理程序的这个例子是从的 MSDN

This example of event-handler below is taken from MSDN:

private void dataGridView2_DataError(object sender, DataGridViewDataErrorEventArgs     anError)
{

    MessageBox.Show("Error happened " + anError.Context.ToString());

    if (anError.Context == DataGridViewDataErrorContexts.Commit)
    {
        MessageBox.Show("Commit error");
    }
    if (anError.Context == DataGridViewDataErrorContexts.CurrentCellChange)
    {
        MessageBox.Show("Cell change");
    }
    if (anError.Context == DataGridViewDataErrorContexts.Parsing)
    {
        MessageBox.Show("parsing error");
    }
    if (anError.Context == DataGridViewDataErrorContexts.LeaveControl)
    {
        MessageBox.Show("leave control error");
    }

    if ((anError.Exception) is ConstraintException)
    {
        DataGridView view = (DataGridView)sender;
        view.Rows[anError.RowIndex].ErrorText = "an error";
        view.Rows[anError.RowIndex].Cells[anError.ColumnIndex].ErrorText = "an error";

        anError.ThrowException = false;
    }
}

和它添加到你的 DataGridView的实例:

dataGridView2.DataError += dataGridView2_DataError;

您可以获取有关的这个资源

这篇关于处理的dataGridView FormatExeption上编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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