2013-05-31 140 views
1

我犯了一个错误handelder,它将在我输入无效的时间或日期格式(如12:456和55-32-1986)时激活。然后程序应该将单元格值更改回prev值。以编程方式无法编辑dataGridView单元格的值

我使用.NET 4.5,这是一个WinForms应用程序

代码:

dataGridView2.DataError += dataGridView2_DataError; 

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

    if ((anError.Exception) is FormatException) 
    { 
     if (dataGridView2.CurrentCell == dataGridView2.CurrentRow.Cells[3]) 
     { 
      MessageBox.Show("Please enter a valid time value" + prevTime); 
      dataGridView2.CurrentCell.Value = prevTime; 
      dataGridView2.EndEdit(); 
     } 
     if (dataGridView2.CurrentCell == dataGridView2.CurrentRow.Cells[2]) 
     { 
      MessageBox.Show("Please enter a valid date"); 
      dataGridView2.CurrentCell.Value = prevDate; 
      dataGridView2.EndEdit(); 
     } 
    } 

    //cell types 
    d.Tables.Add(booking); 
     booking.Columns.Add("nr.", typeof(int)); 
     booking.Columns.Add("Name", typeof(string)); 
     booking.Columns.Add("Date", typeof(DateTime)); 
     booking.Columns.Add("Time", typeof(DateTime)); 

    //Datasource 
    dataGridView2.DataSource = booking; 
} 

prevDate-和TIMEVALUE在datagrid_onBeginEdit声明,好让我的价值观。

我有prev值。代码从头到尾运行。但是从第2行开始,它不会以编程方式更改单元格的值。我只能做到这一点, ,当代码不能改变的值,那么错误消息框不断出现。 datagridview不是只读的。

Cells:nr。 /姓名/日期/时间

任何帮助将不胜感激,在此先感谢。

编辑:我添加了事件的完整代码handeler

PS。如果我在某个地方不清楚,或者如果你需要更多的信息,那就告诉你需要什么。

+0

也许这对其他人更清楚,但是,你的UI框架是什么? WPF?的WinForms?的WebForms? – Jay

+0

你在哪里打电话给你的事件处理程序?这是来自'DataGridView'上的特定事件吗?如果是这样,哪一个? –

回答

3

你不需要自己保存prevDate和prevTime,我已经尝试恢复值,但似乎我们不能改变它们(我仍然兴奋地发现如何在上下文中改变它们) 。但我在这里为你提供了最好的解决方案,我们只需要使用DataGridView的方法CancelEdit(),它应该是你想要的,尽管它不显示如何更改你的问题中描述的上下文中的单元格值。下面是代码:

if ((anError.Exception) is FormatException) 
{ 
    if (dataGridView2.CurrentCell == dataGridView2.CurrentRow.Cells[3]) 
    { 
     MessageBox.Show("Please enter a valid time value" + prevTime); 
     dataGridView2.CancelEdit();//Only this works 
    } 
    if (dataGridView2.CurrentCell == dataGridView2.CurrentRow.Cells[2]) 
    { 
     MessageBox.Show("Please enter a valid date"); 
     dataGridView2.CancelEdit();//Only this works 
    } 
} 

我希望它能帮助,再一次,我还是兴奋如何改变在这方面的单元格值。如果我找到了解决办法,我会把它作为你的另一个答案发布。谢谢!

+0

这只是完美的,非常感谢 – MasterXD

相关问题