ASP.NET使用GridView分组相似的行的数据
GridView可以有分组功能,首先看下这次做的效果图:
实现以上GridView分组相似行的效果源代码如下:
private int prevParentID = 0; //用于判断某一行的数据与上一行的数据的某个特征值是否发生了变化,从而确定是否应该插入分组行。
protected void gvProblems_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView rowView = (DataRowView) e.Row.DataItem;
int parentID = (int) rowView["ParentID"];
if (parentID != prevParentID)
{
//insert row
Table tbl = (Table) gvProblems.Controls[0];
int newRowIndex = tbl.Rows.GetRowIndex(e.Row);
GridViewRow newRow = new GridViewRow(newRowIndex, newRowIndex, DataControlRowType.DataRow,
DataControlRowState.Normal);
TableCell cell = new TableCell();
cell.Text = UIHelper.GetIssueTreeParentData(parentID);
cell.CssClass = "parent_tree_group";
newRow.Cells.Add(cell);
cell.ColumnSpan = gvProblems.Columns.Count;
tbl.Controls.AddAt(newRowIndex, newRow);
//
prevParentID = parentID;
}
}
}