namespace CommonClassLibrary
{
publicclass GroupedGridView : GridView
{
//设置合并的列数
publicint GroupedDepth
{
get
{
object val =this.ViewState["GroupedDepth"];
if (null== val)
{
return0;
}
return (int)val;
}
set
{
if (value <0)
thrownew ArgumentOutOfRangeException("value", "value must be greater than or equal to zero");
this.ViewState["GroupedDepth"] = value;
}
}
protectedoverridevoid OnDataBound(EventArgs e)
{
base.OnDataBound(e);
this.SpanCellsRecursive(0, 0, this.Rows.Count);
}
privatevoid SpanCellsRecursive(int columnIndex, int startRowIndex, int endRowIndex)
{
if (columnIndex >=this.GroupedDepth || columnIndex >=this.Columns.Count)
return;
TableCell groupStartCell =null;
int groupStartRowIndex = startRowIndex;
for (int i = startRowIndex; i < endRowIndex; i++)
{
TableCell currentCell =this.Rows[i].Cells[columnIndex];
bool isNewGroup = (null== groupStartCell) || (0!= String.CompareOrdinal(currentCell.Text, groupStartCell.Text));
if (isNewGroup)
{
if (null!= groupStartCell)
{
SpanCellsRecursive(columnIndex +1, groupStartRowIndex, i);
}
groupStartCell = currentCell;
groupStartCell.RowSpan =1;
groupStartRowIndex = i;
}
else
{
currentCell.Visible =false;
groupStartCell.RowSpan +=1;
}
}
SpanCellsRecursive(columnIndex +1, groupStartRowIndex, endRowIndex);
}
}
}