[英]How to handle two datagridviews errors with one method?
I have task to create method which have to handle two DataGridView
errors and display them in screen. 我有创建方法的任务,必须处理两个DataGridView
错误并在屏幕上显示它们。
I have this method which works perfecetly when using one datagridview, but I how can handle both datagridviews errors with one method? 我有这个方法在使用一个datagridview时完美地工作,但我怎么能用一个方法处理datagridviews错误?
public void gridErr(object sender, DataGridViewDataErrorEventArgs e)
{
// Don't throw an exception when we're done.
e.ThrowException = false;
// Display an error message.
string txt = "Klaida su " + dataGridView1.Columns[e.ColumnIndex].HeaderText + "\n\n" + e.Exception.Message;
MessageBox.Show(txt, "Klaida", MessageBoxButtons.OK, MessageBoxIcon.Error);
// If this is true, then the user is trapped in this cell.
e.Cancel = false;
}
My friend told that it can be done by not using events. 我的朋友说,可以通过不使用事件来完成。
That code would work perfectly for 2 different controls, apart from the explicit reference to dataGridView1. 除了对dataGridView1的显式引用之外,该代码可以完美地用于2个不同的控件。 You should change that to refer to a grid variable which is inferred from the sender: 您应该更改它以引用从发件人推断出的网格变量:
var grid = (DataGridView)sender;
Then that method can work as an error handler for any grid. 然后该方法可以作为任何网格的错误处理程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.