0

I have hosted a custom control in datagridview, let's me say CustomControl (third party control), and cell is painting only when entering in edit mode. If exit edit mode, it is not visible so I have overrided the paint method (see below). It is working ok in windows 7 but not in windows xp. DrawToBitmap is failing. Any ideas?

        protected override void Paint(
        Graphics graphics,
        Rectangle clipBounds,
        Rectangle cellBounds,
        int rowIndex,
        DataGridViewElementStates cellState,
        object value,
        object formattedValue,
        string errorText,
        DataGridViewCellStyle cellStyle,
        DataGridViewAdvancedBorderStyle advancedBorderStyle,
        DataGridViewPaintParts paintParts)         {             // Call the base class method to paint the default​ cell appearance.
        base.Paint(
            graphics,
            clipBounds,
            cellBounds,
            rowIndex,
            cellState,
            value,
            formattedValue,
            errorText,
            cellStyle,
            advancedBorderStyle,
            paintParts);

        CustomControl customControl= (CustomControl)this.DataGridView.Rows[rowIndex].Cells[this.ColumnIndex].Value;

        Bitmap img = new Bitmap(cellBounds.Width, cellBounds.Height);

        // Resize propertyEditor control to cell bounds
        propertyEditor.Height = cellBounds.Hei​ght;
        propertyEditor.Width = cellBounds.Widt​h;

        // Sets the cell's backcolor according to the data​ grid view's color
        customControl.BackColor = this.DataGridView.Rows[rowIndex].Cells[this.ColumnIndex].Style.BackColor;

        // Finally paint the propertyEditor control       ​     
        customControl.DrawToBitmap(img, new Rectangle(0, 0, customControl.Width, customControl.Height​));
        graphics.DrawImage(img, cellBounds.Loc​ation);
    }
Willy
  • 9,848
  • 22
  • 141
  • 284

1 Answers1

1

I was able to reproduce a really similar issue with Windows 7 by removing the row Application.EnableVisualStyles() from my Program.cs. I was using a simple ComboBox not something third party but the effect was the same. To overcome the issue I had to do three things:

  1. Set the (custom)control to visible true so that DrawToBitmap will surely be able to render it
  2. Set the control's parent to the DataGridView (e.g. customControl.Parent = DataGridView) so that it has a parent that's also visible
  3. Move the control out of the visible area (e.g. customControl.Location = new Point(0, -(customControl.Height)) so the control isn't showing up where it shouldn't

This worked in my case but it may depend on how your custom control handles the DrawToBitmap function.

I'm curious if this will help in your case or if anyone can find a more elegant solution.

CseCsa
  • 11
  • 1