2011-09-13 78 views
2

我有下面的代碼的datagridview運行的事件:防止從一個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]); 
    } 

我使用_continueCellEdit防止CellEndEdit事件運行。我可以用這個來禁用/啓用事件:

datagridview_CustomerList.CellEndEdit += datagridview_CustomerList_CellEndEdit; 

這不幫助,因爲我沒有把上面一行的地方。如果我將它放在datagridview_CustomerList_CellMouseDoubleClick中,它仍然在此事件結束後運行。

我得太多概率這個,因爲我沒能採取的午餐,我要怪的東西,但...

是否有處理這個,而不是使用一個布爾更好的辦法?

謝謝!

回答

1

可以使用chain of responsibility模式,但老實說,我不認爲你的使用權證的情況下它和布爾解決方案是好的。

這就是說它可以實現這樣的事情

鏈的第一個處理程序將基本上看起來像

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) 
    } 

} 

和第二處理程序看起來像

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; 

} 
} 
} 

然後你可以寫下面的內容

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

哈哈是啊我想我會堅持布爾。感謝您的替代想法。這在一些其他問題上給了我一些想法。 – ErocM