现在的位置: 首页 > 综合 > 正文

Winform下DataGridView评分单元格的痛苦折磨

2017年11月07日 ⁄ 综合 ⁄ 共 767字 ⁄ 字号 评论关闭

先上最终效果图:

小小的一个功能折磨了哥近半个小时。

首先是没有替换模板,新加一个列,然后妄想给这列的单元格赋值能达到效果,结果formattedValue和value都为空,好吧。我认了。

直接给列的CellTemplate赋值,

dataGridView1.Columns[2].CellTemplate = new DataGridViewTextBoxCellStar();

然后出现了下列问题:

是的,仔细一看,原本的数字还是存在的,只是上面几个有星星,把数字给遮住了,但是当有一个为0时,则会暴露出数字。

首先想到了擦除,所以:

graphics.FillRectangle(new SolidBrush(this.DataGridView.BackgroundColor), cellBounds);

结果很悲剧:

想来此this.DataGridView.BackgroundColor非彼DataGridView.BackgroundColor。

突然想到一个偷懒的办法,设置文本的颜色。好吧。

dataGridView1.Columns[2].DefaultCellStyle.ForeColor = dataGridView1.BackgroundColor;

结果很好,但一点给暴露了:

仔细对比这个图和第一、二个图,发现只有选择的时候,数字的颜色显示了出来。

最后我怒了。真想写事件,切换文本颜色。但是觉得那样做太2了。

结果,让我想到了最后的解决办法,也把我打击了一下。

base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, "", errorText, cellStyle, advancedBorderStyle, paintParts);

是的,不让他画文字……

我认为我很悲剧。

抱歉!评论已关闭.