0

i'm displaying some rows in DatagridView which will have first column as ImageandText.when user selects any row i would like to select till the cell with text portion by keeping the image background as white.

Code used for displaying the image and text in first cell of DataGridView :

private void dgvLogDetails_CellPainting(object sender, 
                                            DataGridViewCellPaintingEventArgs e)
    {

        if (e.RowIndex >= 0 && e.ColumnIndex == 0)
        {                
            e.PaintBackground(e.ClipBounds, true);
            PointF p = e.CellBounds.Location;
            e.CellStyle.SelectionBackColor = Color.White;
            p.X += imgList.ImageSize.Width+8;


            e.Graphics.DrawImage(imgList.Images[1], e.CellBounds.X+4, 
                                                 e.CellBounds.Y, 16, 16);
            e.Graphics.DrawString(e.Value.ToString(), 
                                            e.CellStyle.Font, Brushes.Black, p);
            e.Handled = true;
        }

    }

the above code selects the complete cell (image and text) as shown in below picture: enter image description here

i wanted to have something like in below picture [Expected]:

enter image description here

Tried Sriram code and it is showing as below:

enter image description here

Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67

1 Answers1

1

Something like this should do the trick.

Call e.PaintBackground with second parameter set to false which means it won't draw the selection background for you, then you can draw your own.

private void dgvLogDetails_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.RowIndex >= 0 && e.ColumnIndex == 0)
    {
        Bitmap image = imgList.Images[1];//Get the image somewhow

        bool selected = e.State.HasFlag(DataGridViewElementStates.Selected);
        e.PaintBackground(e.ClipBounds, false);

        PointF p = e.CellBounds.Location;
        p.X += image.Size.Width + 8;

        if (selected)
        {
            RectangleF newRect = new RectangleF(new PointF(e.CellBounds.Left + image.Size.Width, e.CellBounds.Top), new SizeF(e.CellBounds.Width - image.Size.Width, image.Height));
             using(SolidBrush brush = new SolidBrush(e.CellStyle.SelectionBackColor))
                 e.Graphics.FillRectangle(brush, newRect);
        }
        e.Graphics.DrawImage(imgList.Images[1], e.CellBounds.X+4, 
                                             e.CellBounds.Y, 16, 16);
        e.Graphics.DrawString(e.Value.ToString(), 
                                        e.CellStyle.Font, Brushes.Black, p);
        e.Handled = true;
    }
}

Here's the output it renders:

enter image description here

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • Thank you very much Sriram, i have tried but still it's not unselecting the image properly, i have attached sample image in my edited post. – Sudhakar Tillapudi Sep 19 '14 at 12:04
  • I think you're handling other paint events and doing some other paintings. Otherwise this should work, I just tested with a simple data it worked as expected. Cross check what is your `ImageSize` etc. If you can't figure out, post a sample project which I can look into, by that I mean I should be able to run it to see the problem (including images also). I'll try to help. – Sriram Sakthivel Sep 19 '14 at 12:07
  • Yes :) its working fine after changing the height and width in if block, Thank you Sriram. – Sudhakar Tillapudi Sep 19 '14 at 12:18