我在 datagridview 中托管了一个自定义控件,比如说 CustomControl(第三方控件),并且只有在进入编辑模式时才会绘制单元格。如果退出编辑模式,它是不可见的,所以我已经覆盖了绘制方法(见下文)。它在 windows 7 中可以正常工作,但在 windows xp 中却不行。DrawToBitmap 失败。有任何想法吗?
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.Height;
propertyEditor.Width = cellBounds.Width;
// 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.Location);
}