datagridview合并单元格文字居中
时间: 2023-04-02 14:00:39 浏览: 140
可以使用以下代码实现:
```
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex == -1 && e.ColumnIndex > -1)
{
e.PaintBackground(e.CellBounds, true);
e.Graphics.RotateTransform(-90.F);
e.Graphics.DrawString(dataGridView1.Columns[e.ColumnIndex].HeaderText, dataGridView1.Font, Brushes.Black, new RectangleF(e.CellBounds.Y, e.CellBounds.X, e.CellBounds.Height, e.CellBounds.Width), new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
e.Graphics.RotateTransform(90.F);
e.Handled = true;
}
else if (e.ColumnIndex == -1 && e.RowIndex > -1)
{
e.PaintBackground(e.CellBounds, true);
e.Graphics.DrawString((e.RowIndex + 1).ToString(), dataGridView1.Font, Brushes.Black, e.CellBounds, new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
e.Handled = true;
}
}
```
这段代码可以实现将 DataGridView 中的行头和列头进行合并,并且文字居中显示。