winform datagrid 重绘某一列时选中背景不变

由于需求,需要实现在datagrid的一列中实现多个不等的图片,重绘实现后发现重绘列选中颜色不变,很是难受,希望各位大佬多多指点
效果图:

clipboard.png

代码:
重新绘制部分:

using (Brush gridBrush = new SolidBrush(dataGrid.GridColor), backColorBrush = new SolidBrush(e.CellStyle.BackColor))
            {
                using (Pen gridLinePen = new Pen(gridBrush, 5))
                {
                    e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
                    for (int i = 0; i < images.Count; i++ )
                    {
                        Rectangle newRect = new Rectangle(e.CellBounds.X + 3 + i * 48, e.CellBounds.Y + 3, 
                            48, 48);
                        e.Graphics.DrawImage(images[i], newRect);
                    }
                    e.Handled = true;
                }
            }

datagrid 样式部分:

newDataGrid.AllowUserToAddRows = false;
newDataGrid.AllowUserToDeleteRows = false;
newDataGrid.AllowUserToResizeRows = false;
newDataGridViewCellStyle1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
newDataGrid.AlternatingRowsDefaultCellStyle = newDataGridViewCellStyle1;
newDataGrid.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
newDataGrid.BackgroundColor = System.Drawing.Color.Gainsboro;
newDataGrid.BorderStyle = System.Windows.Forms.BorderStyle.None;
newDataGrid.CellBorderStyle = System.Windows.Forms.DataGridViewCellBorderStyle.None;
newDataGrid.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.None;
newDataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
newDataGridViewCellStyle2.BackColor = System.Drawing.Color.SeaGreen;
newDataGridViewCellStyle2.Font = new System.Drawing.Font("Microsoft Sans Serif", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
newDataGridViewCellStyle2.ForeColor = System.Drawing.Color.White;
newDataGridViewCellStyle2.SelectionBackColor = System.Drawing.Color.Transparent;
newDataGridViewCellStyle2.SelectionForeColor = System.Drawing.Color.Transparent;
newDataGridViewCellStyle2.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
newDataGrid.ColumnHeadersDefaultCellStyle = newDataGridViewCellStyle2;
newDataGrid.ColumnHeadersHeight = 40;
newDataGrid.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
newDataGrid.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
newColumn1,
newColumn2,
newColumn3,
newColumn4,
newColumn5,
newColumn6,
newColumn7,
clientId});
newDataGrid.Dock = System.Windows.Forms.DockStyle.Fill;
newDataGrid.DoubleBuffered = true;
newDataGrid.EnableHeadersVisualStyles = false;
newDataGrid.HeaderBgColor = System.Drawing.Color.FromArgb(((int)(((byte)(60)))), ((int)(((byte)(141)))), ((int)(((byte)(188)))));
newDataGrid.HeaderForeColor = System.Drawing.Color.White;
newDataGrid.Location = new System.Drawing.Point(0, 0);
newDataGrid.Name = "newBunifuCustomDataGrid1";
newDataGrid.RowHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.None;
newDataGrid.RowHeadersVisible = false;
newDataGrid.RowHeadersWidthSizeMode = System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;
newDataGridViewCellStyle5.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
newDataGrid.RowsDefaultCellStyle = newDataGridViewCellStyle5;
newDataGrid.RowTemplate.Height = 50;
newDataGrid.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
newDataGrid.Size = new System.Drawing.Size(781, 487);
newDataGrid.TabIndex = 0;
newDataGrid.CellDoubleClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.bunifuCustomDataGrid1_CellDoubleClick);
newDataGrid.CellPainting += new System.Windows.Forms.DataGridViewCellPaintingEventHandler(this.newDataGrid_CellPainting);

            
阅读 2.7k
1 个回答

如果是自主绘制单元格背景,应该还判断下当前单元格是否被选中:datagridview[col,row].Selected
然后根据选中与否,选择e.CellStyle.BackColor或SelectionColor

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题