2

I thought PaintParts indicates which parts should be painted by default. It seems to work OK but when the DataGridViewCell is selected, everything is painted by default. I just want to paint everything but the Content, here is my code:

private void dataGridView_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e){
   e.PaintParts = DataGridViewPaintParts.All & ~DataGridViewPaintParts.ContentForeground;
}

It works OK when the cell is not selected, however if I select a cell, it's painted by default with all background and content. The default/standard DataGridView works OK but I'm dealing with a custom/third party DataGridView.

Could you please explain to me what that is and give me some solution for that?

Thanks a lot!

King King
  • 61,710
  • 16
  • 105
  • 130

2 Answers2

1

I believe you could just specify that everything be painted except the Content Foreground in this fashion.

I have done this and it works.

This should be exactly what you need unless you need only specific cells to not have all their paintParts painted.

private void dataGridView_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
    e.PaintCells(e.ClipBounds, DataGridViewPaintParts.ContentBackground | DataGridViewPaintParts.Border | DataGridViewPaintParts.Background | DataGridViewPaintParts.SelectionBackground);
    e.Handled = true;

    //The e.Handled = true tells the event handler that the event has been completed and that the system doesn't need to do anymore processing.  This line is required to ensure it doesn't process any further(paint more stuff).
}

ps. just found this

C# DataGridViewCheckBoxColumn Hide/Gray-Out

Community
  • 1
  • 1
patL
  • 53
  • 5
  • As I said in my question my code does work for the standard DataGridView in .NET but it doesn't work for the custom DataGridView I'm working with. And of course that's why your code also doesn't work. I don't think there is an easy solution for this because the DataGridView is customized hardly for others to change how it renders. If you want to try, just test your code with a DataGridViewX (in DotNetBar), not with the standard DataGridView. Thanks! – King King May 31 '13 at 13:25
0

for DataGridViewX need to turn off the office 2007 enhancement

try

this.nameofyoudatagridview.PaintEnhancedSelection = false;

when you set the properties

did this work for you?

Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107