DataGridView合并后的单元格不能被选中
零度遗忘 2009-11-03 09:41:55 下面是合并datagridview单元格的代码,但有个缺陷,和并后的单元格在选择方式为行选的不能被选中,
各位大虾请帮帮忙吧
private static SortedList rowSpan = new SortedList();//取得需要重新绘制的单元格
private static string rowValue = "";//重新绘制的文本框内容
public static void MerageRowSpan(DataGridView dgv, DataGridViewCellPaintingEventArgs cellArgs, int minColIndex, int maxColIndex)
{
if (cellArgs.RowIndex != -1 && dgv.Rows[cellArgs.RowIndex].Cells[minColIndex].Value.ToString() == dgv.Rows[cellArgs.RowIndex].Cells[maxColIndex].Value.ToString())
{
if (cellArgs.ColumnIndex < minColIndex || cellArgs.ColumnIndex > maxColIndex) return;
Rectangle rect = new Rectangle();
using (Brush gridBrush = new SolidBrush(dgv.GridColor),
backColorBrush = new SolidBrush(cellArgs.CellStyle.BackColor))
{
//抹去原来的cell背景
cellArgs.Graphics.FillRectangle(backColorBrush, cellArgs.CellBounds);
}
cellArgs.Handled = true;
if (rowSpan[cellArgs.ColumnIndex] == null)
{
//首先判断当前单元格是不是需要重绘的单元格
//保留此单元格的信息,并抹去此单元格的背景
rect.X = cellArgs.CellBounds.X;
rect.Y = cellArgs.CellBounds.Y;
rect.Width = cellArgs.CellBounds.Width;
rect.Height = cellArgs.CellBounds.Height;
rowValue = cellArgs.Value.ToString();
rowSpan.Add(cellArgs.ColumnIndex, rect);
if (cellArgs.ColumnIndex != maxColIndex)
return;
MeragePrint(dgv, cellArgs, minColIndex, maxColIndex);
}
else
{
IsPostMerage(dgv, cellArgs, minColIndex, maxColIndex);
}
}
}
/// <summary>
/// 不是初次单元格绘制
/// </summary>
/// <param name="dgv"></param>
/// <param name="cellArgs"></param>
/// <param name="minColIndex"></param>
/// <param name="maxColIndex"></param>
public static void IsPostMerage(DataGridView dgv, DataGridViewCellPaintingEventArgs cellArgs, int minColIndex, int maxColIndex)
{
//比较单元是否有变化
Rectangle rectArgs = (Rectangle)rowSpan[cellArgs.ColumnIndex];
if (rectArgs.X != cellArgs.CellBounds.X || rectArgs.Y != cellArgs.CellBounds.Y
|| rectArgs.Width != cellArgs.CellBounds.Width || rectArgs.Height != cellArgs.CellBounds.Height)
{
rectArgs.X = cellArgs.CellBounds.X;
rectArgs.Y = cellArgs.CellBounds.Y;
rectArgs.Width = cellArgs.CellBounds.Width;
rectArgs.Height = cellArgs.CellBounds.Height;
rowSpan[cellArgs.ColumnIndex] = rectArgs;
}
MeragePrint(dgv, cellArgs, minColIndex, maxColIndex);
}
//画制单元格
private static void MeragePrint(DataGridView dgv, DataGridViewCellPaintingEventArgs cellArgs, int minColIndex, int maxColIndex)
{
int width = 0;//合并后单元格总宽度
int height = cellArgs.CellBounds.Height;//合并后单元格总高度
for (int i = minColIndex; i <= maxColIndex; i++)
{
width += ((Rectangle)rowSpan[i]).Width;
}
Rectangle rectBegin = (Rectangle)rowSpan[minColIndex];//合并第一个单元格的位置信息
Rectangle rectEnd = (Rectangle)rowSpan[maxColIndex];//合并最后一个单元格的位置信息
//合并单元格的位置信息
Rectangle reBounds = new Rectangle();
reBounds.X = rectBegin.X;
reBounds.Y = rectBegin.Y;
reBounds.Width = width - 1;
reBounds.Height = height - 1;
using (Brush gridBrush = new SolidBrush(dgv.GridColor),
backColorBrush = new SolidBrush(cellArgs.CellStyle.BackColor))
{
using (Pen gridLinePen = new Pen(gridBrush))
{
if (cellArgs.Value.ToString() != dgv.Rows[cellArgs.RowIndex].Cells[cellArgs.ColumnIndex + 1].Value.ToString())
{
//右侧的线
if (cellArgs.ColumnIndex == 3)
cellArgs.Graphics.DrawLine(gridLinePen, cellArgs.CellBounds.Right - 1, cellArgs.CellBounds.Top,
cellArgs.CellBounds.Right - 1, cellArgs.CellBounds.Bottom - 1);
}
//上边缘的线
cellArgs.Graphics.DrawLine(gridLinePen, cellArgs.CellBounds.Left, cellArgs.CellBounds.Top - 1, cellArgs.CellBounds.Right - 1, cellArgs.CellBounds.Top - 1);
//下边缘的线
cellArgs.Graphics.DrawLine(gridLinePen, cellArgs.CellBounds.Left, cellArgs.CellBounds.Bottom - 1, cellArgs.CellBounds.Right - 1, cellArgs.CellBounds.Bottom - 1);
//计算绘制字符串的位置
SizeF sf = cellArgs.Graphics.MeasureString(rowValue, cellArgs.CellStyle.Font);
float lstr = (width - sf.Width) / 2;
float rstr = (height - sf.Height) / 2;
//画出文本框
if (cellArgs.Value.ToString() != "")
{
cellArgs.Graphics.DrawString(cellArgs.Value.ToString(), cellArgs.CellStyle.Font, new SolidBrush(cellArgs.CellStyle.ForeColor),
rectBegin.Left + lstr, rectBegin.Top + rstr, StringFormat.GenericDefault);
}
}
cellArgs.Handled = true;
}
}