views:

350

answers:

1

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

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.

Jage