可以使用ASP.Net中的GroupTemplate属性来实现在gridView中创建数据组。下面是一个示例:
1.在aspx页面中添加gridView控件和SqlDataSource控件。
<%# Eval("Country") %>
2.在code-behind中添加RowDataBound事件处理程序。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { string country = DataBinder.Eval(e.Row.DataItem, "Country").ToString(); if (country != "") { if (prevCountry != country) { if (count > 0) { e.Row.Visible = false; prevCountry = country; } else { count++; prevCountry = country; } } } } }
以上代码可以将具有相同“Country”值的行分成一组,并在GridView的上方显示组标题