【问题标题】:Get current cell text from a DataGridView, but when an DataError is caught从 DataGridView 获取当前单元格文本,但是当捕获到 DataError 时
【发布时间】:2021-07-19 01:56:59
【问题描述】:

上下文:WinForms 和 C#

我正在尝试使用用户尝试输入的文本显示MessageBox,例如,如果该列是整数类型列,他会得到一个MessageBox 说:

您输入的值“Abcdef”与此列类型不匹配

我知道如何获取列类型,但如何获取该文本?

private void dgv_Datos_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
    MessageBox.Show("The value you tried (" + dgv_Datos.CurrentCell.Value.ToString() + ") doesn't match with this column type");
}

问题是CurrentCell.Value.ToString() 仅在前面的文本正确的情况下才有效。如果是 Byte 列,您输入 100,然后更改为“Abcdef”,则会在消息框错误中得到“100”。

谢谢!

【问题讨论】:

标签: c# winforms datagridview


【解决方案1】:

试试这个:

private void dgv_Datos_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
    MessageBox.Show("The value you tried (" + dgv_Datos.EditingControl.Text + ") doesn't match with this column type");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-10
    • 2023-03-06
    • 2010-10-17
    • 1970-01-01
    • 1970-01-01
    • 2019-01-09
    • 1970-01-01
    相关资源
    最近更新 更多