private void dgvUutSetting_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
DataGridView dataGridView = (DataGridView)sender;
if (e.ColumnIndex >= 0 && e.RowIndex != -1)
{
using (Brush gridBrush = new SolidBrush(dataGridView.GridColor), backColorBrush = new SolidBrush(e.CellStyle.BackColor))
{
using (Pen gridLinePen = new Pen(gridBrush))
{
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
if (e.RowIndex < dataGridView.Rows.Count - 1 && dataGridView.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString() != e.Value.ToString())
{
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
}
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);
if (e.Value != null)
{
if (!(e.RowIndex > 0 && dataGridView.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value.ToString() == e.Value.ToString()))
{
e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,Brushes.Black, e.CellBounds.X + 2,e.CellBounds.Y + 5, StringFormat.GenericDefault);
}
}
e.Handled = true;
}
}
}
}
WinForm DataGridView合并内容相同的单元格
于 2023-10-25 16:37:28 首次发布