本文介绍了在C#中合并RTL Datagridview列标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要合并3个Datagridview列标题(第3,第4和第5个
I want to merge 3 Datagridview columns headers (the 3rd, 4th, and the 5th
列),并启用Datagridview的RightToleft属性。我用户
columns) and the RightToleft property of the Datagridview is enabled. i user
此代码:
private void PromotionButton_Click(object sender, EventArgs e)
{
dataGridView1.ColumnHeadersHeight = dataGridView1.ColumnHeadersHeight * 2;
dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
dataGridView1.CellPainting += new DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);
dataGridView1.Paint += new PaintEventHandler(dataGridView1_Paint);
dataGridView1.Scroll += new ScrollEventHandler(dataGridView1_Scroll);
dataGridView1.ColumnWidthChanged += new DataGridViewColumnEventHandler(dataGridView1_ColumnWidthChanged);
}
private void dataGridView1_Paint(object sender, PaintEventArgs e)
{
for (int j = 2; j < 5; j++)
{
Rectangle r1 = dataGridView1.GetCellDisplayRectangle(j, -1, true);
int w2 = dataGridView1.GetCellDisplayRectangle(j + 1, -1, true).Width;
r1.X += 1;
r1.Y += 1;
r1.Width = r1.Width + w2 - 2;
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);
}
void dataGridView1_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
{
Rectangle rtHeader = dataGridView1.DisplayRectangle;
rtHeader.Height = dataGridView1.ColumnHeadersHeight / 2;
dataGridView1.Invalidate(rtHeader);
}
void dataGridView1_Scroll(object sender, ScrollEventArgs e)
{
Rectangle rtHeader = dataGridView1.DisplayRectangle;
rtHeader.Height = dataGridView1.ColumnHeadersHeight / 2;
dataGridView1.Invalidate(rtHeader);
}
void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex == -1 && e.ColumnIndex > -1)
{
Rectangle r2 = e.CellBounds;
r2.Y += e.CellBounds.Height / 2;
r2.Height = e.CellBounds.Height / 2;
e.PaintBackground(r2, true);
e.PaintContent(r2);
e.Handled = true;
}
}
但结果不像我想要,就像这样:
But the result was not like what i want, it was like this:
那么怎么解决呢?
推荐答案
假设您希望这三列与合并文本的一个打印合并,而您要合并列索引2-4:
Assuming you want those three columns merged with only one print of the merge text, and you want to merge columns indexed 2-4:
- 删除循环。
- 获取宽度所有三个所需的列(而不是列
j
和j + 1
) - 由于您的网格已启用
RightToLeft
,所以最左侧列(第4列,而不是2列)启动矩形。
- Remove the loop.
- Get the width of all three desired columns (instead of column
j
andj+1
) - Start your Rectangle at the left-most column (column 4, not 2) since your grid has
RightToLeft
enabled.
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);
}
此外,我建议使用以下对齐方式来阻止您的头文本部分阻塞:
Also, I would suggest using the following alignment to prevent your header text from partial obstruction:
dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter;
这篇关于在C#中合并RTL Datagridview列标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!