我正在尝试合并DataGridView
中的奇数行headecell,这似乎可行,但是当鼠标移到单元格上方时,它们可能坏了
代码来了,如何改进呢?谢谢!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DGVRowHeaderCellMerge
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.dataGridView1.Scroll += (s, e) => this.dataGridView1.Invalidate();
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.GetType().InvokeMember("DoubleBuffered", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.SetProperty, null, dataGridView1, new object[] { true });
dataGridView1.EnableHeadersVisualStyles = false;
dataGridView1.RowHeadersWidth = 160;
dataGridView1.ColumnHeadersHeight = 40;
dataGridView1.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.White;
dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = Color.Black;
dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
for (int i = 0; i < 50; i++)
{
DataGridViewColumn dc = new DataGridViewColumn();
dataGridView1.Columns.Add("ch" + i.ToString(), "C" + i.ToString());
dataGridView1.Columns[i].Width = 30;
dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
dataGridView1.Columns[i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.BottomCenter;
this.dataGridView1.Columns[i].Resizable = DataGridViewTriState.False;
Application.DoEvents();
}
//dataGridView1.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.None;
dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
for (int j = 0; j < 100; j++)
{
int idx = dataGridView1.Rows.Add();
dataGridView1.Rows[idx].HeaderCell.Value = "R" + j.ToString();
dataGridView1.Rows[idx].HeaderCell.Style.WrapMode = DataGridViewTriState.True;
dataGridView1.Rows[idx].HeaderCell.Style.SelectionBackColor = Color.White;
dataGridView1.Rows[idx].HeaderCell.Style.BackColor = Color.White;
dataGridView1.Rows[idx].Resizable = DataGridViewTriState.False;
Application.DoEvents();
}
}
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
if (e.RowIndex == -1) return;
int rem = -1;
Math.DivRem(e.RowIndex + 1, 2, out rem);
if (rem == 0)
{
int i = e.RowIndex - 2;
Rectangle r1 = this.dataGridView1.GetCellDisplayRectangle(-1, i, true);
r1.X += 1;
r1.Y += 1;
r1.Width = r1.Width;
r1.Height = r1.Height * 2;
e.Graphics.FillRectangle(new SolidBrush(Color.LightYellow), r1);
StringFormat format = new StringFormat()
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
};
e.Graphics.DrawString(i.ToString(),
this.dataGridView1.ColumnHeadersDefaultCellStyle.Font,
new SolidBrush(this.dataGridView1.RowHeadersDefaultCellStyle.ForeColor),
r1,
format);
Pen p = new Pen(Color.Red);
Point pTopLeft = new Point(r1.X - 1, r1.Y - 2);
Point pTopRight = new Point(r1.X + r1.Width - 2, r1.Y - 2);
e.Graphics.DrawLine(p, pTopLeft, pTopRight);
Point pBotRight = new Point(r1.X + r1.Width - 2, r1.Y + r1.Height);
e.Graphics.DrawLine(p, pTopRight, pBotRight);
}
}
}
}
答案 0 :(得分:0)
this.dataGridView1.RowValidating += (s, e) => this.dataGridView1.Invalidate();
添加它,现在可以了。