防止为数据视图运行事件
本文关键字:运行 事件 视图 数据 | 更新日期: 2023-09-27 18:08:25
我有一个数据视图,代码如下:
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来防止cellenddit事件的运行。我可以用它来禁用/启用事件:
datagridview_CustomerList.CellEndEdit += datagridview_CustomerList_CellEndEdit;
这没有用,因为我没有地方放上面的行。如果我把它放在datagridview_CustomerList_CellMouseDoubleClick中,它仍然在此事件完成后运行。
我可能想太多了,因为我没有吃午饭,我必须责怪一些事情,但是…
是否有更好的方法来处理这个而不是使用bool?
谢谢!
您可以使用责任链模式,但老实说,我认为您的用例不需要它,bool解决方案很好。
可以这样实现
链中的第一个处理程序基本上看起来像
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;