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

查看:23
本文介绍了有没有办法强制 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 之前以编程方式滚动到网格的所有部分.只比 (1) 稍微不那么可怕

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 是我们从 dot 开始就采用的方式.

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.

然而,这里的新问题是在加载期间应用单元格样式格式似乎没有任何效果.如果你谷歌一下,那里有很多帖子.他们还指出,如果您将相同的代码放在表单上的按钮下并在加载后单击它(而不是在加载中,代码将起作用......因此网格必须在样式应用之前可见).大多数有关该主题的建议都建议您使用...鼓声... CellFormatting.啊!

最终找到了一篇建议使用网格的 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天全站免登陆