本人想删除如图所示,蓝色处的分隔符。要怎么实现啊。
希望有人能够帮本人看看,谢谢!
解决方案
40
在 DataGridView.CellPainting 事件中,利用 Graphics 自定义绘制
private void dgv_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { var dgv = sender as DataGridView; var g = e.Graphics; if (e.RowIndex < 0) { if (Enumerable.Range(0, 3).Contains(e.ColumnIndex)) { Brush bg = new SolidBrush(dgv.ColumnHeadersDefaultCellStyle.BackColor); // 背景色 Brush fg = new SolidBrush(dgv.ColumnHeadersDefaultCellStyle.ForeColor); // 前景色 if ((e.PaintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background) g.FillRectangle(bg, e.CellBounds); // 绘制 Header 的背景色 if ((e.PaintParts & DataGridViewPaintParts.ContentBackground) == DataGridViewPaintParts.ContentBackground) g.FillRectangle(SystemBrushes.Window, e.CellBounds); // 绘制 Content 的背景色 if ((e.PaintParts & DataGridViewPaintParts.ContentForeground) == DataGridViewPaintParts.ContentForeground) { var col = dgv.Columns[e.ColumnIndex]; var text = col?.HeaderText ?? ""; var sizeF = g.MeasureString(text, dgv.Font); // 测量绘制列标题文本尺寸 var x = e.CellBounds.X + (e.CellBounds.Width - sizeF.Width) / 2f; // 获取 Left var y = e.CellBounds.Y + (e.CellBounds.Height - sizeF.Height) / 2f; // 获取 Top g.DrawString(text, dgv.Font, fg, x, y); // 绘制列标题 // 绘制上下边框 g.DrawLine(Pens.Red, e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Right, e.CellBounds.Y); g.DrawLine(Pens.Red, e.CellBounds.X, e.CellBounds.Height, e.CellBounds.Right, e.CellBounds.Height); col.Resizable = DataGridViewTriState.False; // 不允许调整列宽 } e.Handled = true; } } }