DataGridView CellPainting无法完全在滚动条上工作 [英] DataGridView CellPainting Not Fully Working on Scroll

查看:67
本文介绍了DataGridView CellPainting无法完全在滚动条上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一篇文章,但浏览器却使用了很长时间:)

First post, but long time browser :)

这是我的问题:基本上,我有一个datagridview,可用于与用户进行交互。在所有行中,都有一个信息按钮,会给人一种错觉,即它会在其下方添加另一行并合并所有单元格(整个行中有一个长单元格),并绘制描述其上一行的文本和图像。到信息单元上。

So here's my problem: Basically I have a datagridview that I am using to interact with the user. In all of the rows, there is an "info" button that will give the illusion that it adds another row below it with all of the cells merged (one long cell across entire row) and draws the text and images describing the row above it onto the "info cell".

这很好用,除了当datagridview垂直滚动时,看上去好像没有画出来,网格看起来很混乱。有什么想法吗?

This works great except when the datagridview is scrolled vertically, then it appears that the painting isn't called and the grid is messed up looking. Any ideas?

这是代码的基本轮廓:

private void grid_CellPainting(object sender, DataGridViewCellPaintingEventArgs e){     
    // Loop through and draw all of the open information rows
    foreach (int i in openInfoCells) {
        if (i >= grid.FirstDisplayedCell.RowIndex && 
            i <= (grid.DisplayedRowCount(true) + grid.FirstDisplayedCell.RowIndex)) {

            // Draw Rectangle
            ....

            // Draw Text or Image
            ....
        }
    }
}


推荐答案

弄清楚了。需要通过执行以下操作为datagridview创建一个双缓冲区:

Figured it out. Needed to create a double buffer for the datagridview by doing the following:

class CustomDataGridView : DataGridView {

public CustomDataGridView() {
    base.DoubleBuffered = true;
}
}

我还将代码放入RowPostPainting事件中并进行了更改if语句:

I also put the code into the RowPostPainting event and changed the if statement to:

if (e.RowIndex == i) { .... }

希望这可以帮助其他人。

Hopefully this helps somebody else out.

这篇关于DataGridView CellPainting无法完全在滚动条上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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