2个回答
展开全部
C#DataGridView合并单元格:
public void dgCellPainting(DataGridView dgStandard, string strColumnName, DataGridViewCellPaintingEventArgs e)
{
if (dgStandard.Rows.Count > 0)
{
if (dgStandard.Rows[0].Cells[0].Value.ToString().Trim() != string.Empty)
{
try
{
if (dgStandard.Columns[strColumnName].Index == e.ColumnIndex && e.RowIndex >= 0)
{
using (
Brush gridBrush = new SolidBrush(dgStandard.GridColor),
backColorBrush = new SolidBrush(e.CellStyle.BackColor))
{
using (Pen gridLinePen = new Pen(gridBrush))
{
// 擦除原单元格背景
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
if (e.RowIndex != dgStandard.RowCount - 1)
{
if (e.Value.ToString() != dgStandard.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString())
{
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);//下边缘的线
}
}
else
{
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, //x1,y1,x2,y2
e.CellBounds.Top, e.CellBounds.Right - 1,
e.CellBounds.Bottom - 1);
if (e.RowIndex == 0)
{
//绘制值
if (e.Value != null)
{
e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
Brushes.Crimson,
e.CellBounds.X +(e.CellBounds.Width - e.Graphics.MeasureString(e.Value.ToString().Trim(), e.CellStyle.Font).Width)/2,
e.CellBounds.Y + 2, StringFormat.GenericDefault);
}
}
else
{
if (e.Value.ToString() != dgStandard.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value.ToString())
{
//绘制值
if (string.IsNullOrEmpty(e.Value.ToString().Trim()).Equals(false))
{
e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
Brushes.Crimson, e.CellBounds.X + (e.CellBounds.Width - e.Graphics.MeasureString(e.Value.ToString().Trim(), e.CellStyle.Font).Width) / 2,
e.CellBounds.Y + 2, StringFormat.GenericDefault);
}
}
}
e.Handled = true;
}
}
}
}
catch { }
}
}
}
参考:
http://bbs.csdn.net/topics/310116954
public void dgCellPainting(DataGridView dgStandard, string strColumnName, DataGridViewCellPaintingEventArgs e)
{
if (dgStandard.Rows.Count > 0)
{
if (dgStandard.Rows[0].Cells[0].Value.ToString().Trim() != string.Empty)
{
try
{
if (dgStandard.Columns[strColumnName].Index == e.ColumnIndex && e.RowIndex >= 0)
{
using (
Brush gridBrush = new SolidBrush(dgStandard.GridColor),
backColorBrush = new SolidBrush(e.CellStyle.BackColor))
{
using (Pen gridLinePen = new Pen(gridBrush))
{
// 擦除原单元格背景
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
if (e.RowIndex != dgStandard.RowCount - 1)
{
if (e.Value.ToString() != dgStandard.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString())
{
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);//下边缘的线
}
}
else
{
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, //x1,y1,x2,y2
e.CellBounds.Top, e.CellBounds.Right - 1,
e.CellBounds.Bottom - 1);
if (e.RowIndex == 0)
{
//绘制值
if (e.Value != null)
{
e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
Brushes.Crimson,
e.CellBounds.X +(e.CellBounds.Width - e.Graphics.MeasureString(e.Value.ToString().Trim(), e.CellStyle.Font).Width)/2,
e.CellBounds.Y + 2, StringFormat.GenericDefault);
}
}
else
{
if (e.Value.ToString() != dgStandard.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value.ToString())
{
//绘制值
if (string.IsNullOrEmpty(e.Value.ToString().Trim()).Equals(false))
{
e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
Brushes.Crimson, e.CellBounds.X + (e.CellBounds.Width - e.Graphics.MeasureString(e.Value.ToString().Trim(), e.CellStyle.Font).Width) / 2,
e.CellBounds.Y + 2, StringFormat.GenericDefault);
}
}
}
e.Handled = true;
}
}
}
}
catch { }
}
}
}
参考:
http://bbs.csdn.net/topics/310116954
更多追问追答
追问
我说的是我想只合并第二列的值为b的单元格,你这个写出来是直接判断一列中相同值的就合并,这个方法我早看过了,没用
追答
原理都是相同的,都是先擦除原来的线,再根据条件绘制上下左右的线;我抽空做个Demo试试;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
和table一样
追问
有demo么?能发出来我看看吗
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询