有没有办法使row.DefaultCellStyle.BackColor固定,尽pipe重绘?

有没有办法使row.DefaultCellStyle.BackColor固定,尽pipe重绘?,第1张

概述有没有办法使row.DefaultCellStyle.BackColor固定,尽pipe重绘?

现在我正在使用以下颜色我的datagrIDvIEw行:

foreach (DataGrIDVIEwRow row in dataGrIDVIEw1.Rows) { if ((row.Index % 2) == 0) { row.DefaultCellStyle.Backcolor = color.NavajoWhite; } }

这对于第一次加载数据时是很好的。 不过,我也使用第三方库来筛选像Excel这样的列( http://www.codeproject.com/Articles/33786/DataGrIDVIEw-Filter-Popup )。 它工作得很好,但问题是,这个代码重新绘制 datagrIDvIEw每个应用的过滤(白色)。 例如,如果我愿意,可以在每次过滤之后捕获必要的事件来重新绘制行

dataGrIDVIEw1_RowPostPaint(object sender,DataGrIDVIEwRowPostPaintEventArgs e)

但是,如果我有大型matrix(数十万行)是非常低效的。

问题是,是否有办法使行背景颜色固定 ,尽pipe过滤完成重绘。 在我看来,这可能是一个远射,所以任何build议来解决这个问题,或使其更快,更有效率,将不胜感激。

WPF应用程序中的“属性元素不能位于元素内容的中间”错误

如何在树形视图中获取当前选定的节点

以编程方式打印文档

如何以编程方式login到窗口7,8桌面

PowerShell如何使按键上的及时更改?

如何为xbuild添加缺less的程序集引用? (单声道/ linux)的

空隙的.NET代码签名的应用程序不会安装/运行

如何避免windows崩溃屏幕

Mono在linux上的当前状态?

在特定的桌面上启动进程

尝试仅处理每个单元格的CellPainting事件

private voID dataGrIDVIEw1_CellPainting(object sender,System.windows.Forms.DataGrIDVIEwCellPaintingEventArgs e) { if ((e.RowIndex % 2) == 0) e.CellStyle.Backcolor = color.NavajoWhite; }

自己尝试一下MSDN的例子之后,我发现它并不是那么容易,我从事DataGrIDVIEw的工作已经很长时间了。

下面的例子提供了备用的行背景,不管单元格的风格噱头。 只需在窗体上放置一个网格,然后在Form的构造函数中调用这个方法:

private voID InitializeGrID() { dgv.Columns.Add("colID","ID"); dgv.Columns.Add("colname","name"); for (int c = 1; c <= 100; c++) { int r = dgv.Rows.Add(); dgv.Rows[r].SetValues(c,"Person" + c); } dgv.SelectionMode = DataGrIDVIEwSelectionMode.FullRowSelect; }

它将创建一个示例网格,您可以修补。

这里有一点有趣的地方:

private voID dgv_RowPrePaint(object sender,DataGrIDVIEwRowPrePaintEventArgs e) { // Don't paint focused/selected rows if ((e.State & DataGrIDVIEwElementStates.Selected) == DataGrIDVIEwElementStates.Selected) return; // This informs the event that we don't want it to paint the background,we'll take // care of it instead. e.PaintParts &= ~DataGrIDVIEwPaintParts.Background; // Calculate row rectangle (based off the MSDN example) var rowBounds = new Rectangle( dgv.RowheadersWIDth,e.RowBounds.top,dgv.Columns.GetColumnsWIDth(DataGrIDVIEwElementStates.Visible) - dgv.HorizontalScrollingOffset + 1,e.RowBounds.Height); // Paint row headers,for some reason they are part of the Background. e.Paintheader(true); // Now custom-paint the row background using (Brush backBrush = new SolIDBrush(color.NavajoWhite)) { if (e.RowIndex % 2 != 0) // If RowIndex is not divisible by 2 then paint custom color e.Graphics.FillRectangle(backBrush,rowBounds); else // Otherwise just let the grID paint the row e.PaintCells(rowBounds,DataGrIDVIEwPaintParts.Background); } }

确保你也有using System.Drawing; 指示。 请注意,这个例子将只适用于dgv.SelectionMode = DataGrIDVIEwSelectionMode.FullRowSelect; 。 对于常规的细胞选择模式来说,这有点麻烦,你也必须处理这种情况。

渲染速度非常快,可以帮助您完成任务。 如果没有,那么至少你有更多的选择来考虑。 在适当的时候更改单元格的样式也应该可以正常工作,但可能会导致闪烁或影响响应。

总结

以上是内存溢出为你收集整理的有没有办法使row.DefaultCellStyle.BackColor固定,尽pipe重绘?全部内容,希望文章能够帮你解决有没有办法使row.DefaultCellStyle.BackColor固定,尽pipe重绘?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1280432.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存