简体   繁体   English

阻止事件为datagridview运行

[英]Prevent an event from running for a datagridview

I have a datagridview with the following code: 我有一个带有以下代码的datagridview:

    private void datagridview_CustomerList_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        if (!_continueCellEdit)
        {
            _continueCellEdit = true;
            return;
        }

        if (datagridview_CustomerList.Rows[e.RowIndex].Cells[e.ColumnIndex].OwningColumn.Name == ColumnNames.NewRateColumn.ToString())
        {
            var row = datagridview_CustomerList.Rows[e.RowIndex];
            var font = datagridview_CustomerList.Font;

            if (modMain.SSS_ToDecimal(row.Cells[ColumnNames.NewRateColumn.ToString()].Value) > 0)
            {
                row.DefaultCellStyle.Font = new Font(font, FontStyle.Regular);
                if (modMain.SSS_ToDecimal(row.Cells[ColumnNames.BudgetBalanceColumn.ToString()].Value) > 0)
                    row.DefaultCellStyle.BackColor = color_BudgetCustomers;
                else
                    row.DefaultCellStyle.BackColor = color_OriginalColor;
            }
            else
            {
                row.DefaultCellStyle.BackColor = color_ZeroCharge;
                row.DefaultCellStyle.Font = new Font(font, FontStyle.Strikeout);
            }
        }

    }

    private void datagridview_CustomerList_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        _continueCellEdit = false;
        PaintRow(datagridview_CustomerList.Rows[e.RowIndex]);
    }

I am using the _continueCellEdit to prevent the CellEndEdit event from running. 我正在使用_continueCellEdit阻止CellEndEdit事件运行。 I can use this to disable/enable the event: 我可以使用它来禁用/启用事件:

datagridview_CustomerList.CellEndEdit += datagridview_CustomerList_CellEndEdit;

This doesn't help cause I don't have a place to put the above line. 这无济于事,因为我没有放置上述内容的位置。 If I put it in the datagridview_CustomerList_CellMouseDoubleClick it still runs after this event has finished. 如果我将其放在datagridview_CustomerList_CellMouseDoubleClick中,则此事件完成后仍将运行。

I am prob overthinking this since I didn't get to take lunch, I gotta blame something but... 我没有考虑过这个问题,因为我没吃午饭,我要怪一些事情,但是...

Is there a better way of handling this instead of using a bool? 有没有比使用布尔值更好的方法来处理此问题?

Thanks! 谢谢!

You could use the chain of responsibility pattern, but I honestly don't think your use case warrants it and the bool solution is fine. 可以使用责任链模式,但是老实说,我不认为您的用例有必要,而且布尔解决方案还不错。

That said it could be implemented something like this 就是说它可以像这样实现

The first handler in the chain would basically look like 链中的第一个处理程序基本上看起来像

 public DoubliClickHanlder : IChain
 {
    public IChain NextHandler(get;set;)

 public void ProcessEvent(object sender, DataGridViewCellEventArgs e)
    {
      if !(this.continueCellEdit && this.NextHandler!= null)
          NextHandler.ProcessEvent(sender,e) 
    }

 }

and the second handler would look like 第二个处理程序看起来像

public GridColorSetter : IChain
{
 public IChain NextHandler(get;set;)
public void ProcessEvent(object sender, DataGridViewCellEventArgs e)
{
       if (datagridview_CustomerList.Rows[e.RowIndex].Cells[e.ColumnIndex].OwningColumn.Name == ColumnNames.NewRateColumn.ToString())
        {
            var row = datagridview_CustomerList.Rows[e.RowIndex];
            var font = datagridview_CustomerList.Font;

            if (modMain.SSS_ToDecimal(row.Cells[ColumnNames.NewRateColumn.ToString()].Value) > 0)
            {
                row.DefaultCellStyle.Font = new Font(font, FontStyle.Regular);
                if (modMain.SSS_ToDecimal(row.Cells[ColumnNames.BudgetBalanceColumn.ToString()].Value) > 0)
                    row.DefaultCellStyle.BackColor = color_BudgetCustomers;
                else
                    row.DefaultCellStyle.BackColor = color_OriginalColor;
            }
            else
            {
                row.DefaultCellStyle.BackColor = color_ZeroCharge;
                row.DefaultCellStyle.Font = new Font(font, FontStyle.Strikeout);
            }
        }

    if (this.NextHandler!= null)
      this.NextHandler.ProcessEvent;

 }
}
}

Then it you could write the following 然后,您可以编写以下内容

DoubleClickHandler dch = new DoubleClickHandler () {NextHandler= new GridColorSetter()}
this.datagridview_CustomerList.CellEndEdit += dch.ProcessEvent;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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