DataGridView CellPainting Not Fully Working on Scroll

心已入冬 提交于 2019-12-31 03:02:44

问题


First post, but long time browser :)

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".

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?

Here is a basic outline of the code:

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
            ....
        }
    }
}

回答1:


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

class CustomDataGridView : DataGridView {

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

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.



来源:https://stackoverflow.com/questions/1144083/datagridview-cellpainting-not-fully-working-on-scroll

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!