0

Hello i have problem when multiple click in a cells fire out the system event CellContentClick.

When a checkbox is true an amount have to be sum to the cell under the header called "Monto" and when is unchecked the amount have to be substracted.

Everything goes fine if the user do the click in a slowly way, but if he does rapidly the amount goes crazy. How to handle this situation? I tried to make the cell readonly iniside of the event but didn't work.

Here is a picture of the data grid view

enter image description here

Here is my code of the events handler

private void comissionsDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (employeeComissionsDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].IsInEditMode)
    {
        employeeComissionsDataGridView.CellContentClick -= new DataGridViewCellEventHandler(comissionsDataGridView_CellContentClick);
        double amountOfDay = employeeOperations.comissionsEmployeeGetAmount(employeeComissionsDataGridView.Rows[e.RowIndex].Cells[3].Value.ToString(), (e.ColumnIndex - 3).ToString());

        double amount = 0;
        try
        {
            amount = Double.Parse(employeeComissionsDataGridView.Rows[e.RowIndex].Cells[35].Value.ToString());
        }
        catch
        {
            amount = 0;
        }

        if (employeeComissionsDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "True")
        {
            amount += amountOfDay;
            employeeComissionsDataGridView.Rows[e.RowIndex].Cells[35].Value = amount;
        }
        else
        {
            amount -= amountOfDay;
            if (amount < 0) //Decimals handle
                amount = 0;
            employeeComissionsDataGridView.Rows[e.RowIndex].Cells[35].Value = amount;
        }
        employeeComissionsDataGridView.CellContentClick += new DataGridViewCellEventHandler(comissionsDataGridView_CellContentClick);
    }
}

private void employeeComissionsDataGridView_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
    if (employeeComissionsDataGridView.IsCurrentCellDirty)
    {
        employeeComissionsDataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
    }
}
  • Read the notes here: [Programmatically check a DataGridView CheckBox that was just unchecked](https://stackoverflow.com/a/62031996/7444103). It's not really useful to unsubscribe to an event in the event handler and then subscribe to it again in the same *place*. You don't want to use the `CurrentCellDirtyStateChanged` event to commit. You have seen this used somewhere else, of course: nonetheless, avoid it. – Jimi Jul 29 '20 at 16:48
  • Note that you could also use a Calculated Column, if you're using a DataTable (see the [DataColumn.Expression](https://learn.microsoft.com/en-us/dotnet/api/system.data.datacolumn.expression) and, for example, [here](https://stackoverflow.com/a/62291110/7444103)) – Jimi Jul 29 '20 at 16:56

1 Answers1

0

It is better to handle the CellValueChanged event.

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1) return;

            var row = dataGridView1.Rows[e.RowIndex];
            var cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];

            if (cell.IsInEditMode)
            {
                double amountOfDay = 123;// employeeOperations.comissionsEmployeeGetAmount(row.Cells[3].Value.ToString(), (e.ColumnIndex - 3).ToString());

                double amount = 0;
                double.TryParse(string.Format($"{row.Cells[35].Value}", "{0:F}"), out amount);

                if (cell.Value is bool val && val == true)
                {
                    amount += amountOfDay;
                    row.Cells[35].Value = amount;
                }
                else
                {
                    amount -= amountOfDay;
                    if (amount < 0) //Decimals handle
                        amount = 0;
                    row.Cells[35].Value = amount;
                }
            }
        }

enter image description here

CobyC
  • 2,058
  • 1
  • 18
  • 24