有没有办法强制DataGridView为其所有单元格激活其CellFormatting事件? [英] Is there a way to force a DataGridView to fire its CellFormatting event for all cells?

查看:257
本文介绍了有没有办法强制DataGridView为其所有单元格激活其CellFormatting事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用CellFormatting事件来代替我们应用程序中各种网格中的单元格。

We use the CellFormatting event to colour code cells in various grids all over our application.

我们有一些通用代码来处理导出到Excel(和打印),但它在Black&白色。现在我们要改变这个,并从网格中提取颜色。

We've got some generic code which handles export to Excel (and print) but it does it in Black & White. Now we want to change this and pick up the colour from the grids.

这个问题&答案已经帮助(它的工作)...除了大型网格超出单个屏幕的问题。网格中尚未显示在屏幕上的部分(逻辑上)从未得到它们的CellFormatting代码的触发,因此它们的基本颜色从未被设置。因此,在Excel中,颜色编码在页面的下半部分。

This question & answer has helped (and it works) ... except there's a problem with larger grids that extend beyond a single screen. The portions of the grid which haven't yet been displayed on screen are (logically) never getting their CellFormatting code fired, and so their underlying colour never gets set. As a result, in Excel, the colour coding fizzles out half way down the page.

似乎有三种解决方案:

1)在执行导出到Excel之前,告诉用户他必须滚动到网格的所有部分。哈!不是一个严肃的解决方案

1) Tell the user he has to scroll to all parts of the grid before doing an Export to Excel. Ha! Not a serious solution

2)在进行导出到Excel之前,以编程方式滚动到网格的所有部分。

2) Programmatically scroll to all parts of the grid before doing an Export to Excel. Only slighly less horrible than (1)

3)在导出到Excel代码中,点击顶部的某些东西,告诉DataGridView进行绘制/格式化其整个区域,例如

3) In our Export to Excel code, fire something at the top which tells the DataGridView to paint/format its entire area e.g.

  MyDataGridView.FormatAllCells()

有没有这样的东西?

哦,有第四个选项,但这将涉及大量现有的代码:

Oh, and there is a fourth option but this will involve touching a massive amount of existing code:

4)停止使用CellFormatting事件,在加载时格式化单元格。问题在于,我们必须重新整理我们的应用程序中的每个网格,因为CellFormatting是从年底开始的方式。

4) Stop using CellFormatting event, format the cells at load time. Problem with this is we'd have to retool every grid in our application since CellFormatting is the way we've done it since year dot.

推荐答案

假设,如@DavidHall所示,没有魔术 .FormatAllCells 我们唯一的选择是停止使用CellFormatting。

Assuming, as @DavidHall suggests, there is no magic .FormatAllCells our only option is to stop using CellFormatting.

然而,这里的新问题是在加载期间应用单元格样式格式化似乎没有任何效果。如果您有Google,那么很多帖子。他们还指出,如果您将相同的代码放在窗体上的按钮上,并在加载后点击它(而不是在加载中,则代码将正常工作...因此网格必须在样式可应用之前可见)。关于该主题的大多数建议建议您使用... drumroll ... CellFormatting。 Aargh!

最终找到一个建议使用网格的 DataBindingComplete 事件的帖子。这个工作。

Eventually found a post which suggests using the DataBindingComplete event of the grid. And this works.

诚然,这个解决方案是我不想要的选项4的变体。

Admittedly, this solution is a variant of my unwanted option "4".

这篇关于有没有办法强制DataGridView为其所有单元格激活其CellFormatting事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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