源代码:
///<summary>
/// Web合并相同数据的行标题
///</summary>
///<param name="gv"></param>
///<param name="col"></param>
///<param name="colName"></param>
public static void Unite(GridView gv, int[] col, string[] colName)
{
int i;
string LastType1;//主键第一列字段名
string LastType2;
int LastCell;
if (gv.Rows.Count > 0)
{
for (int j = 0; j < col.Length; j++)
{
LastType1 = gv.Rows[0].Cells[int.Parse(colName[0].ToString())].Text;
LastType2 = gv.Rows[0].Cells[int.Parse(colName[j].ToString())].Text;
gv.Rows[0].Cells[col[j]].RowSpan = 1;
LastCell = 0;
for (i = 1; i < gv.Rows.Count; i++)
{
if (gv.Rows[i].Cells[int.Parse(colName[j].ToString())].Text == LastType2 && gv.Rows[i].Cells[int.Parse(colName[0].ToString())].Text == LastType1)
{
gv.Rows[i].Cells[col[j]].Visible = false;
gv.Rows[LastCell].Cells[col[j]].RowSpan++;
}
else
{
LastType1 = gv.Rows[i].Cells[int.Parse(colName[0].ToString())].Text;
LastType2 = gv.Rows[i].Cells[int.Parse(colName[j].ToString())].Text;
LastCell = i;
gv.Rows[i].Cells[col[j]].RowSpan = 1;
}
}
}
}
}
///<summary>
/// Web设置指定行合并列
///</summary>
///<param name="gv"></param>
///<param name="pRowIndex">要合并列的行号</param>
public static void UniteColumn(GridView gv, int pRowIndex,int pColIndex,int pSpanCount)
{
gv.Rows[pRowIndex].Cells[pColIndex].ColumnSpan = pSpanCount;
for (int i = 1; i < pSpanCount; i++)
{
gv.Rows[pRowIndex].Cells[pColIndex + i].Visible = false;
}
gv.Rows[pRowIndex].Cells[pColIndex].Width = Unit.Pixel(0);
}
///<summary>
/// Winform合并相同数据的行标题
///</summary>
///<param name="gv"></param>
///<param name="col"></param>
///<param name="colName"></param>
public static void Unite(DataGridView gv, int[] col, string[] colName)
{
int i;
string LastType1;//主键第一列字段名
string LastType2;
int LastCell;
if (gv.Rows.Count > 0)
{
for (int j = 0; j < col.Length; j++)
{
LastType1 = gv.Rows[0].Cells[int.Parse(colName[0].ToString())].Value.ToString();
LastType2 = gv.Rows[0].Cells[int.Parse(colName[j].ToString())].Value.ToString();
((gv.Rows[0].Cells[col[j]]) as DataGridViewTextBoxCellEx).RowSpan = 1;
LastCell = 0;
for (i = 1; i < gv.Rows.Count; i++)
{
if (gv.Rows[i].Cells[int.Parse(colName[j].ToString())].Value.ToString() == LastType2 && gv.Rows[i].Cells[int.Parse(colName[0].ToString())].Value.ToString() == LastType1)
{
//gv.Rows[i].Cells[col[j]].Visible = false;
(gv.Rows[LastCell].Cells[col[j]] as DataGridViewTextBoxCellEx).RowSpan++;
}
else
{
LastType1 = gv.Rows[i].Cells[int.Parse(colName[0].ToString())].Value.ToString();
LastType2 = gv.Rows[i].Cells[int.Parse(colName[j].ToString())].Value.ToString();
LastCell = i;
(gv.Rows[i].Cells[col[j]] as DataGridViewTextBoxCellEx).RowSpan = 1;
}
}
}
}
}
///<summary>
/// DataGridView设置指定行合并列
///</summary>
///<param name="pDataGridView"></param>
///<param name="pRowIndex">要合并列的行号</param>
///<param name="pColumnIndex">起始列索引数</param>
///<param name="pColumnNumber">合并列数量</param>
///<param name="pBackColor">背景色彩</param>
public static void SetColumnMerge(DataGridView pDataGridView, int pRowIndex, int pColumnIndex, int pColumnNumber, Color pBackColor)
{
var cell = (DataGridViewTextBoxCellEx)pDataGridView[pColumnIndex, pRowIndex];
cell.ColumnSpan = pColumnNumber;
cell.RowSpan = 1;
cell.Style.BackColor = pBackColor;
}
调用:
UnitGVTitle.UniteColumn(gv, rowCount, 0, 3);
UnitGVTitle.UniteColumn(gv, rowCount, 3, 1);
UnitGVTitle.UniteColumn(gv, rowCount, 4, 3);
效果: