简体   繁体   中英

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.

I have this method which works perfecetly when using one datagridview, but I how can handle both datagridviews errors with one method?

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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