如何检测CellFormatting事件中通过e.CellStyle.BackColor应用于DataGridView的样式? [英] How to detect styling applied to DataGridView via e.CellStyle.BackColor in the CellFormatting event?

查看:139
本文介绍了如何检测CellFormatting事件中通过e.CellStyle.BackColor应用于DataGridView的样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们通过DataGridView的CellFormatting事件对某些行应用了样式(例如 e.CellStyle.BackColor ),那么是否有可能在以下位置检测到该样式

If we've applied styling (e.CellStyle.BackColor say) to some rows via the CellFormatting event of a DataGridView, is it then possible to detect that styling at a later stage?

例如,当前,我们使用通用的代码块来处理打印和导出到我们所有DataGridView的Excel。到目前为止,该代码还无法满足任何样式要求。

For example, currently we use a generic bloc of code to handle printing and exporting to Excel for any and all our DataGridViews. Until now the code hasn't catered for any styling.

所以我们要添加它。

如果我们检查行或单元格的 .DefaultCellStyle ,则样式不会显示(仅显示为0或黑色,这是完全错误的)。

If we check the .DefaultCellStyle of the row or cell then our styling doesn't show up (it just shows as 0 or Black, which is completely wrong).

我认为这是因为我们通过CellFormatting事件应用了样式,而不是将其嵌入到DefaultCellStyle中。

I assume that's because we've applied the styling via a CellFormatting event, instead of embedding it into the DefaultCellStyle.

推荐答案

不幸的是,我找不到完整的解决方案来解决您的问题,只能解决。

Unfortunately I could not find a complete solution to your issue, only a work around.

有些人正在尝试使用CellFormatting事件 MSDN 中的示例导致我看到正是您所看到的- BackColor 已明确设置,但 CellStyle 却没有反映出来。 1

Some experimenting with the CellFormatting event using the example from MSDN resulted in me seeing exactly what you were seeing - the BackColor was clearly being set but the CellStyle was not reflecting that. 1

我发现的解决方法是不使用 DataGridViewCellFormattingEventArgs CellStyle 属性,而是直接转到网格。缺点是您现在需要手动处理不想格式化单元格的情况。

The work around I found was to not use the DataGridViewCellFormattingEventArgs CellStyle property but to instead go straight to the grid. This has the downside that you now need to manually handle the case where you do not want to format the cell.

我在下面有一些代码可以显示出来-再次修改MSDN代码:

I've got some code below showing this - it is again just modifying the MSDN code:

void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    // If the column is the Artist column, check the
    // value.
    if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Artist")
    {
        if (e.Value != null)
        {
            // Check for the string "pink" in the cell.
            string stringValue = (string)e.Value;
            stringValue = stringValue.ToLower();
            if ((stringValue.IndexOf("pink") > -1))
            {
                // With the commented line below we cannot access the new style
                //e.CellStyle.BackColor = Color.Pink;                    

                // With this line we can!
                dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Pink;
            }
            else
            {
                // With the original MSDN code the else block to reset the 
                // cell style was not needed.
                dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = dataGridView1.DefaultCellStyle.BackColor;
            }

        }
    }
}






1。我的理论是,这类似于人们对 .Refresh()方法,其中 DataGridView 本身具有两个截然不同的视图,一个是在屏幕上绘制的矩形,另一个是基础数据。使用 .Refresh()方法,您仅重新绘制矩形,而不会刷新数据。我认为是这样- CellFormatting 事件仅在绘画期间格式化,并且对网格样式本身没有任何作用。


1. My theory is that this is similar to the confusion people have over the .Refresh() method, where the DataGridView has two very distinct views of itself, one being the rectangle painted on screen and the other being the underlying data. With the .Refresh() method you only repaint the rectangle, you do not refresh the data. I think this is like that - the CellFormatting event only formats during painting and doesn't do anything to the grid styles themselves.

这篇关于如何检测CellFormatting事件中通过e.CellStyle.BackColor应用于DataGridView的样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆