由于需求,需要实现在datagrid的一列中实现多个不等的图片,重绘实现后发现重绘列选中颜色不变,很是难受,希望各位大佬多多指点
效果图:
代码:
重新绘制部分:
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);
如果是自主绘制单元格背景,应该还判断下当前单元格是否被选中:datagridview[col,row].Selected
然后根据选中与否,选择e.CellStyle.BackColor或SelectionColor