假设您希望这三列仅与
一
打印合并文本,并且要合并索引为2-4的列:
-
拆下回路。
-
获取的宽度
所有三个
所需列(而不是列
j
和
j+1
)
-
从最左边的一列(第4列,而不是第2列)开始矩形,因为你的网格已经
RightToLeft
启用。
private void dataGridView1_Paint(object sender, PaintEventArgs e)
{
Rectangle r1 = dataGridView1.GetCellDisplayRectangle(4, -1, true);
int w2 = dataGridView1.GetCellDisplayRectangle(3, -1, true).Width;
int w3 = dataGridView1.GetCellDisplayRectangle(2, -1, true).Width;
r1.X += 1;
r1.Y += 1;
r1.Width = r1.Width + w2 + w3;
r1.Height = r1.Height / 2 - 2;
e.Graphics.FillRectangle(new SolidBrush(dataGridView1.ColumnHeadersDefaultCellStyle.BackColor), r1);
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
e.Graphics.DrawString("Ø±ÙØ§Ø¶Ùات", dataGridView1.ColumnHeadersDefaultCellStyle.Font,
new SolidBrush(dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor), r1, format);
}
此外,我建议使用以下对齐方式,以防止标题文本部分受阻:
dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter;