鍍金池/ 問答/C++  C#  HTML  Office/ winform datagrid 重繪某一列時選中背景不變

winform datagrid 重繪某一列時選中背景不變

由于需求,需要實現(xiàn)在datagrid的一列中實現(xiàn)多個不等的圖片,重繪實現(xiàn)后發(fā)現(xiàn)重繪列選中顏色不變,很是難受,希望各位大佬多多指點
效果圖:

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);

            
回答
編輯回答
網(wǎng)妓

如果是自主繪制單元格背景,應該還判斷下當前單元格是否被選中:datagridview[col,row].Selected
然后根據(jù)選中與否,選擇e.CellStyle.BackColor或SelectionColor

2018年5月11日 15:52