Prevent an event from running for a datagridview
I have a datagridview with the following code:
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. 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.
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;
精彩评论