有时候,某一行的值重复出现,分组显示更加清晰一些,利用下面的方法进行简单分组,前提是数据查询的SQL语句要GROUP BY 你想分组的字段.
//临时存储分组的值
private string TempYear=string.Empty;
//添加DataGrid_ItemDataBound事件
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
string CurYear = e.Item.Cells[0].Text;
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if(CurYear == TempYear)
{
e.Item.Cells[0].Text = "";
e.Item.Cells[0].Style.Add("Border","0");
}
else
{
TempYear = CurYear;
e.Item.Cells[0].BackColor=System.Drawing.Color.WhiteSmoke;
}
}
}
private string TempYear=string.Empty;
//添加DataGrid_ItemDataBound事件
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
string CurYear = e.Item.Cells[0].Text;
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if(CurYear == TempYear)
{
e.Item.Cells[0].Text = "";
e.Item.Cells[0].Style.Add("Border","0");
}
else
{
TempYear = CurYear;
e.Item.Cells[0].BackColor=System.Drawing.Color.WhiteSmoke;
}
}
}