2

我想根据另一个单元格值或事件动态更改某些特定单元格的前景色和背景色。

例如,当用户单击单元格时,其背景颜色应为红色。

我的代码是这样的:

Janus.Windows.GridEX.GridEXFormatStyle style1 = new GridEX.FormatStyle();

style1.ForeColor = Color.Red;

mySpecificCell.FormatStyle = style1;

它可以工作,但是当我向下滚动然后再次向上滚动时,单元格的颜色会恢复为原始颜色。

我的代码有什么问题?我应该如何克服这个?

4

2 回答 2

4

就像 Arthur 说的,你必须利用网格的 FormattingRow 事件。

这是一个示例代码:

private void grd_FormattingRow(object sender, RowLoadEventArgs e)
{
    if (e.Row.Cells["ColumnName"].Value == someValue) // a condition to determine when to change the color of the cell, you can put your own condition
        e.Row.Cells["ColumnName"].FormatStyle = new GridEXFormatStyle() { BackColor = Color.Red };

}

格式化行将为正在显示的网格中的每一行触发,您可以使用 e.Row 访问该行

“ColumnName”是列的名称。

当您想要更改单元格的颜色时,您可以替换条件来检查。

于 2014-04-15T04:02:24.187 回答
2

尝试使用Gridex 的formattingRow 事件来进行自定义格式设置。

为网格上的每一行调用此事件。

在那里,您可以访问整行。

这意味着您可以检查某个单元格的值,然后根据第一个单元格格式化另一个单元格。

于 2014-04-14T12:59:45.810 回答