[英]Prevent an event from running for a 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中,則此事件完成后仍將運行。
我沒有考慮過這個問題,因為我沒吃午飯,我要怪一些事情,但是...
有沒有比使用布爾值更好的方法來處理此問題?
謝謝!
您可以使用責任鏈模式,但是老實說,我不認為您的用例有必要,而且布爾解決方案還不錯。
就是說它可以像這樣實現
鏈中的第一個處理程序基本上看起來像
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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.