Show selected image in DataGridView using DataGridViewComboBoxColumn?

I have a problem using a DataGridView with a DataGridViewComboBoxColumn so that the user can select an image from a list of images. After discussions in a Question titled "Custom DatagridViewComboBoxColumn Draw" ref Link . I also run into a problem, as the image is only displayed when the cell is in edit mode. The selected image will disappear when I am somewhere outside the combo box! I implemented the CellPainting event to redraw the image, but still cannot solve the problem. I tested a DataGridViewComboBoxColumn with the following codes:

public Form1() { InitializeComponent(); ..... imageList.Images.Add(Properties.Resources.icon_priority_low); imageList.Images.Add(Properties.Resources.icon_priority_medium); ..... } private void Form1_Load(object sender, EventArgs e) { ..... DataGridViewComboBoxCell dgvcbc = (DataGridViewComboBoxCell)newDataGridView1.Rows[0].Cells[1]; dgvcbc.Items.Add("test0"); dgvcbc.Items.Add("test1"); ..... } private void newDataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { if (e.Control is ComboBox) { ComboBox theCB = (ComboBox)e.Control; theCB.DrawMode = DrawMode.OwnerDrawFixed; try { theCB.DrawItem -= combobox1_DrawItem; } catch { } theCB.DrawItem += combobox1_DrawItem; } } private void combobox1_DrawItem(object sender, DrawItemEventArgs e) { Graphics g = e.Graphics; Brush br = SystemBrushes.WindowText; Brush brBack; Rectangle rDraw; bool bSelected = e.State == DrawItemState.Selected; bool bValue = e.State == DrawItemState.ComboBoxEdit; if ((e.Index < 0) || (columnIndex != 1)) return; rDraw = e.Bounds; rDraw.Inflate(-1, -1); int x, y; x = e.Bounds.Left + 25; y = e.Bounds.Top + 1; int midX = (int)(e.Bounds.Width / 2) + e.Bounds.Left; // Show image and ignore text. g.DrawImage(imageList.Images[e.Index], new Rectangle(midX - 6, y + 2, 12, 12)); } private void newDataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { if (columnIndex != 1) return; Graphics g = e.Graphics; Rectangle rDraw = newDataGridView1.GetCellDisplayRectangle(columnIndex, rowIndex, true); e.PaintBackground(e.ClipBounds, true); e.PaintContent(e.ClipBounds); using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor)) { int y = rDraw.Y + 1; int midX = (int)(rDraw.Width / 2) + rDraw.X; g.DrawImage(imageList.Images[0], new Rectangle(midX - 6, y + 2, 12, 12)); e.PaintContent(e.ClipBounds); e.Handled = true; } } } 

The cell will display "test0" instead of images [0] if I click on other cells of the DataGridView. Could you help solve this problem. Many thanks.

+4
source share
1 answer

The last call to PaintContent() erases your framed image.

Before you draw an image, you must draw a cell (but not the front). It will look like this:

 private void newDataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { if (columnIndex != 1) return; Graphics g = e.Graphics; Rectangle rDraw = newDataGridView1.GetCellDisplayRectangle(columnIndex, rowIndex, true); e.Paint(e.CellBounds, e.PaintParts & ~DataGridViewPaintParts.ContentForeground); int y = rDraw.Y + 1; int midX = (int)(rDraw.Width / 2) + rDraw.X; g.DrawImage(imageList.Images[0], new Rectangle(midX - 6, y + 2, 12, 12)); e.Handled = true; } 
0
source

Source: https://habr.com/ru/post/1396561/


All Articles