简体   繁体   中英

C# form, how can I to solve my EventHandler problem?

I am doing a C# form application, I use a timer and every 10 seconds I have to refresh a datagridview with the elements of a list of list (it may happens that some elements of this list can be removed or others added from another function), in each row of this DataGridView I have to add a CellClick event, the problem is that after each update I add an eventHandler to each row and therefore there are multiple eventListener on every row. When the timer runs out I called the following function:

private void Refresh(object sender, EventArgs e)
        {
             tabUsers.Rows.Clear();
             tabUsers.AutoGenerateColumns = false;
             for (int i = 0; i < list1.Count; i++){
                 tabUsers.CellClick += grd_CellClick;
                 tabUsers.Rows.Add(list1[i][0], list1[i][1], list1[i][2])
             } 
        }

grd_CellClick is the event funcion.

You are attaching grd_CellClick as an event handler every refresh. This is unnecessary, just can configure this event handler once (in the constructor of your form).

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