C#Winform中DataGridView合并单元格的问题?
我合并4列单元格位1列,合并好后,当点击前面3个单元格时,会出现如下情况:原来单元格中的内容会显示出来,并且此单元格位选中状态,请问该如何解决,谢谢代码:if(e.Col...
我合并4列单元格位1列,合并好后,当点击前面3个单元格时,会出现如下情况:原来单元格中的内容会显示出来,并且此单元格位选中状态,请问该如何解决,谢谢
代码:if (e.ColumnIndex == 3) {
Rectangle re = new Rectangle(e.CellBounds.Left - dataGridView2.Columns[0].Width - dataGridView2.Columns[1].Width - dataGridView2.Columns[2].Width,
e.CellBounds.Top, e.CellBounds.Width + dataGridView2.Columns[0].Width + dataGridView2.Columns[01].Width + dataGridView2.Columns[2].Width,
e.CellBounds.Height);
e.Graphics.FillRectangle(Brushes.Yellow, re);
Pen pen = new Pen(dataGridView2.BackgroundColor, 1);
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
e.Graphics.DrawLine(pen, re.X, re.Y + re.Height - 1, re.X + re.Width, re.Y + re.Height - 1);
e.Graphics.DrawLine(pen, re.X + re.Width - 1, re.Y, re.X + re.Width - 1, re.Y + re.Height);
SizeF strSize = e.Graphics.MeasureString(dataGridView2.Rows[e.RowIndex].Cells[0].Value.ToString(), dataGridView2.Font);
e.Graphics.DrawString(dataGridView2.Rows[e.RowIndex].Cells[0].Value.ToString(), dataGridView2.Font,
Brushes.Black, re.X + (re.Width - strSize.Width) / 2, re.Y + (re.Height - strSize.Height) / 2); e.Handled = true; }
请注意:我合并的不是表头列,是数据列 展开
代码:if (e.ColumnIndex == 3) {
Rectangle re = new Rectangle(e.CellBounds.Left - dataGridView2.Columns[0].Width - dataGridView2.Columns[1].Width - dataGridView2.Columns[2].Width,
e.CellBounds.Top, e.CellBounds.Width + dataGridView2.Columns[0].Width + dataGridView2.Columns[01].Width + dataGridView2.Columns[2].Width,
e.CellBounds.Height);
e.Graphics.FillRectangle(Brushes.Yellow, re);
Pen pen = new Pen(dataGridView2.BackgroundColor, 1);
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
e.Graphics.DrawLine(pen, re.X, re.Y + re.Height - 1, re.X + re.Width, re.Y + re.Height - 1);
e.Graphics.DrawLine(pen, re.X + re.Width - 1, re.Y, re.X + re.Width - 1, re.Y + re.Height);
SizeF strSize = e.Graphics.MeasureString(dataGridView2.Rows[e.RowIndex].Cells[0].Value.ToString(), dataGridView2.Font);
e.Graphics.DrawString(dataGridView2.Rows[e.RowIndex].Cells[0].Value.ToString(), dataGridView2.Font,
Brushes.Black, re.X + (re.Width - strSize.Width) / 2, re.Y + (re.Height - strSize.Height) / 2); e.Handled = true; }
请注意:我合并的不是表头列,是数据列 展开
1个回答
展开全部
e.ColumnIndex == 3
只会在第三列触发的Paint事件中调用,也就是说,如果点击ColumnIndex =2的单元格触发Paint时,你的代码不起作用,会实用dataGridView2的默认样式绘制.
e.CellBounds,因为上面是第三列所以e.CellBounds始终是第三列的Bounds
--参考
if(e.ColumnIndex>=0&&e.ColumnIndex < 4 && e.RowIndex == 2)
{
Rectangle re =dataGridView1.GetCellDisplayRectangle(0,e.RowIndex,false);
re.Width = re.Width + dataGridView1.Columns[1].Width + dataGridView1.Columns[2].Width + dataGridView1.Columns[3].Width;
e.Graphics.FillRectangle(Brushes.Yellow,re);
Pen pen = new Pen(dataGridView1.BackgroundColor,1);
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
e.Graphics.DrawLine(pen,re.X,re.Y + re.Height - 1,re.X + re.Width,re.Y + re.Height - 1);
e.Graphics.DrawLine(pen,re.X + re.Width - 1,re.Y,re.X + re.Width - 1,re.Y + re.Height);
SizeF strSize = e.Graphics.MeasureString(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString(),dataGridView1.Font);
e.Graphics.DrawString(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString(),dataGridView1.Font,
Brushes.Black,re.X + (re.Width - strSize.Width) / 2,re.Y + (re.Height - strSize.Height) / 2); e.Handled = true;
}
只会在第三列触发的Paint事件中调用,也就是说,如果点击ColumnIndex =2的单元格触发Paint时,你的代码不起作用,会实用dataGridView2的默认样式绘制.
e.CellBounds,因为上面是第三列所以e.CellBounds始终是第三列的Bounds
--参考
if(e.ColumnIndex>=0&&e.ColumnIndex < 4 && e.RowIndex == 2)
{
Rectangle re =dataGridView1.GetCellDisplayRectangle(0,e.RowIndex,false);
re.Width = re.Width + dataGridView1.Columns[1].Width + dataGridView1.Columns[2].Width + dataGridView1.Columns[3].Width;
e.Graphics.FillRectangle(Brushes.Yellow,re);
Pen pen = new Pen(dataGridView1.BackgroundColor,1);
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
e.Graphics.DrawLine(pen,re.X,re.Y + re.Height - 1,re.X + re.Width,re.Y + re.Height - 1);
e.Graphics.DrawLine(pen,re.X + re.Width - 1,re.Y,re.X + re.Width - 1,re.Y + re.Height);
SizeF strSize = e.Graphics.MeasureString(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString(),dataGridView1.Font);
e.Graphics.DrawString(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString(),dataGridView1.Font,
Brushes.Black,re.X + (re.Width - strSize.Width) / 2,re.Y + (re.Height - strSize.Height) / 2); e.Handled = true;
}
追问
可以了,非常感谢
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询