在编辑时处理dataGridView FormatExeption

我正在制作一个包含四列的datagridview。 最后一列的类型是DateTime,以小时和分钟为单位(HH:mm)。

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

当我输入有效的HH:mm(12:37)格式时,它可以正常工作,但如果格式无效,它会给我一个错误信息(12:374)。

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

它告诉我处理“DataError-exeption / FormatExeption”来改变发生错误时会发生什么,但是我该怎么做呢?

我希望它回到错误发生之前的值。

任何帮助将不胜感激。 提前致谢。

PS。 如果我不清楚某个地方,或者您需要更多信息,那么只需解释一下需要什么。

编辑:我正在直接从dataGridView编辑时间值。

处理事件DataGridView.DataError以便您可以正确管理情境并向用户显示正确的消息。

下面的事件处理程序示例来自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.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView_CellFormat); private void dataGridView_CellFormat(Object sender, DataGridViewCellFormattingEventArgs e) { if (dataGridView.Columns[e.ColumnIndex].Name.Equals("Time")) { DateTime dt; if (!DateTime.TryParse(e.Value.ToString(), out dt)) { e.Value = "PreviousValue"; } } } 

我不知道如何得到行的先前值,但这是个主意。

在CellValidating事件中,如果出现错误,您可以设置e.Cancel = true。 看到这里: http : //msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvalidating.aspx

简短的回答是我认为不可能。 我只是用不同的数据类型才有完全相同的问题。 我事件试图在数据错误事件触发之前编辑该值。 从我可以收集到的官方处理方法是吸引用户注意细胞并纠正错误。 有错误文本和错误图标可以帮助解决这个问题。

单元格的新内容可通过单元格的formattedvalue属性获得,但这是只读的,因此无法编辑。

我将参考前面的答案并处理捕获错误类型以向用户显示有意义的错误消息

我最终得到了以下几行代码

 Private Sub dgvInvoices_CellValidating(sender As Object, e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles dgvInvoices.CellValidating dgvInvoices.Rows(e.RowIndex).Cells(3).ErrorText = "" Dim newInteger As Integer dgvInvoices.ShowCellErrors = True If Not Double.TryParse(e.FormattedValue.ToString(), newInteger) OrElse newInteger < 0 Then e.Cancel = True dgvInvoices.Rows(e.RowIndex).Cells(3).ErrorText = "the value must be a non-negative integer" MsgBox("Ensure the value is a numeric value.", vbExclamation, "Error") End If End Sub 

对不起,我可以提供帮助

正如我在这里提到的 – 处理DataGridView.CellValidating事件:

  private void CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { try { // get current cell var cell = ((DataGridView)sender).Rows[e.RowIndex].Cells[e.ColumnIndex]; // check new value. i need any number >= 5 var c0 = 0; if (e.FormattedValue == null || !Int32.TryParse(e.FormattedValue.ToString(), out c0) || c0 < 5) { // bad value inserted // e.FormattedValue - is new value // cell.Value - contains 'old' value // choose any: cell.Value = cell.Value; // this way we return 'old' value e.Cancel = true; // this way we make user not leave the cell until he pastes the value we expect } } catch (Exception ex) { } }