简体   繁体   English

从 DataGridView 获取当前单元格文本,但在捕获 DataError 时

[英]Get current cell text from a DataGridView, but when an DataError is caught

Context: WinForms and C#上下文:WinForms 和 C#

I'm trying to show a MessageBox with the text that user is trying to enter, like for example, if the column is an integer type column, he gets a MessageBox saying:我正在尝试使用用户尝试输入的文本显示MessageBox ,例如,如果该列是整数类型列,他会收到一个MessageBox说:

The value "Abcdef" you entered doesn't match with this column type您输入的值“Abcdef”与此列类型不匹配

I know how to get the column type, but how can I get that text?我知道如何获取列类型,但如何获取该文本?

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

The thing is that CurrentCell.Value.ToString() only works if the previous text was right.问题是CurrentCell.Value.ToString()仅在前面的文本正确时才有效。 If it was a Byte column and you entered 100, and then changed to "Abcdef", you get "100" in the messagebox error.如果它是一个字节列,并且您输入了 100,然后更改为“Abcdef”,则消息框错误中会显示“100”。

Thanks!谢谢!

Try this:试试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM