gpt4 book ai didi

c# - 防止事件为 datagridview 运行

转载 作者:太空宇宙 更新时间:2023-11-03 14:12:32 24 4
gpt4 key购买 nike

我有一个包含以下代码的数据 GridView :

    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 中,它在此事件完成后仍会运行。

我可能想多了,因为我没有吃午饭,我得怪罪什么,但是......

有没有比使用 bool 值更好的处理方法?

谢谢!

最佳答案

可以使用 chain of responsibility模式,但老实说我不认为你的用例保证它并且 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;

关于c# - 防止事件为 datagridview 运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7408873/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com