E
E
Eugene2014-02-12 10:23:26
.NET
Eugene, 2014-02-12 10:23:26

How to simplify work with datagridview?

Hello.
I have, say, 10 DGV objects. For each of this object, the CellValueChanged event is used, where the same type of procedure is used, in which only the names of the DGV objects are different.
Here's what comes out roughly:

private void dgv_cat_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            NewLineAndUpdate_DB(dgv_cat, " ;cat_name;", "tbl_spr_category");
        }

private void dgv_otdel_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            NewLineAndUpdate_DB(dgv_otdel, " ;dep_name;", "tbl_spr_department");
        }
...

and so 10 times.
Next, I use the KeyDown and CellEnter events, which is similarly written 10 times ...
Is it possible to somehow simplify?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem Voronov, 2014-02-12
@gloomkolomna

In this case, you can make a wrapper method:

public static void SubscribeToCellValueChanged(this DataGridView dataGridView, string p1, string p2) 
        {
            dataGridView.CellValueChanged += (sender,e) => NewLineAndUpdate_DB((DataGridView)sender, p1, p2);
        }

Then, in the control initialization method, use this wrapper to sign each DataGridView:
But I would recommend the author to review the current implementation, because. the use of magic strings will break the code with further changes to the database schema.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question