至急!datagrid控件,如何根据某个字段的不同,页面上加一个标题行呢?
数据中有一个地区的字段AREA,存储的数据是A1:Taiwan,A2:Hongkong,A3:China
如何在datagrid中显示为
--------------------------------------------
A1:Taiwan
------------------------------------
product_CD,item_CD,product_name,....
--------------------------------------------
A2:Hongkong
------------------------------------
product_CD1,item_CD,product_name,....
product_CD2,item_CD,product_name,....
product_CD3,item_CD,product_name,....
--------------------------------------------
A3:China
------------------------------------
product_CD5,item_CD,product_name,....
--------------------------------------------
数据已经ORDER BY AREA ,但是AREA这行如何在DATAGRID中加入呢?
至急,清高首赐教!
[解决办法]
我觉得可以分多个GRID,样式做的像一个.方案一.
[解决办法]
表述能不能清楚点。。
A1,A2,A3是表还是。。。
[解决办法]
先顶再想想有没有办法
[解决办法]
你三个DataGrid或者自己写HTML代码了哦.如果你更好的方法麻烦你通知我一下,我以前是用后面的方面做的 QQ172501531
[解决办法]
ItemDataBound 事件中处理,动态插入一行
[解决办法]
学习
[解决办法]
循环一下,依次读取AREA字段
[解决办法]
测试通过,
<%@ Page Language= "C# " %>
<%@ Import Namespace= "System.Data " %>
<%--
http://community.csdn.net/Expert/TopicView3.asp?id=5652860
--%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<script runat= "server ">
private int lastCategoryId = -1; // 私有字段,当前绑定 DataGrid 行的上一行的 CategoryId
void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
LoadProductData();
}
}
protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
DataRowView drv = e.Item.DataItem as DataRowView;
if (drv != null) {
int currentCategoryId = (int)drv[ "CategoryID "];
// 比较当前行 与 上一行 的 CategoryId
if (lastCategoryId != currentCategoryId) {
//
DataGridItem itemCategory = new DataGridItem(-1, -1, ListItemType.Item);
TableCell emptyCell = new TableCell();
emptyCell.Text = GetCategoryName(currentCategoryId);
emptyCell.ColumnSpan = DataGrid1.Columns.Count; // 合并列
itemCategory.Cells.Add(emptyCell);
// 在当前行之前插入一行
DataGrid1.Controls[0].Controls.AddAt(DataGrid1.Controls[0].Controls.Count - 1, itemCategory);
//
lastCategoryId = currentCategoryId;
}
}
}
string GetCategoryName(int categoryId)
{
switch (categoryId) {
case 1 :
return "A1:Taiwan ";
case 2:
return "A2:Hongkong ";
case 3:
return "A3:RPC ";
default:
return "unknown ";
}
}
void LoadProductData()
{
DataTable dt = CreateProductTable();
DataView dv = dt.DefaultView;
dv.Sort = "CategoryID, ProductID ";
DataGrid1.DataSource = dv;
DataGrid1.DataBind();
}
#region sample data
static DataTable CreateProductTable()
{
DataTable tbl = new DataTable( "Products ");
tbl.Columns.Add( "ProductID ", typeof(int));
tbl.Columns.Add( "ProductName ", typeof(string));
tbl.Columns.Add( "CategoryID ", typeof(int));
tbl.Columns.Add( "HasPic ", typeof(bool));
tbl.Columns.Add( "Reviewed ", typeof(bool));
DataRow row = tbl.NewRow();
row[0] = 1;
row[1] = "Chai ";
row[2] = 1;
row[3] = true;
row[4] = false;
tbl.Rows.Add(row);
row = tbl.NewRow();
row[0] = 2;
row[1] = "Chang ";
row[2] = 1;
row[3] = false;
row[4] = false;
tbl.Rows.Add(row);
row = tbl.NewRow();
row[0] = 3;
row[1] = "Aniseed Syrup ";
row[2] = 2;
row[3] = true;
row[4] = false;
tbl.Rows.Add(row);
row = tbl.NewRow();
row[0] = 4;
row[1] = "Chef Anton 's Cajun Seasoning ";
row[2] = 2;
row[3] = false;
row[4] = true;
tbl.Rows.Add(row);
row = tbl.NewRow();
row[0] = 5;
row[1] = "Chef Anton 's Gumbo Mix ";
row[2] = 2;
row[3] = true;
row[4] = true;
tbl.Rows.Add(row);
row = tbl.NewRow();
row[0] = 47;
row[1] = "Zaanse koeken ";
row[2] = 3;
row[3] = true;
row[4] = true;
tbl.Rows.Add(row);
row = tbl.NewRow();
row[0] = 48;
row[1] = "Chocolade ";
row[2] = 3;
row[3] = false;
row[4] = false;
tbl.Rows.Add(row);
row = tbl.NewRow();
row[0] = 49;
row[1] = "Maxilaku ";
row[2] = 3;
row[3] = true;
row[4] = false;
tbl.Rows.Add(row);
return tbl;
}
#endregion
</script>
<html xmlns= "http://www.w3.org/1999/xhtml " >
<head runat= "server ">
<title> CSDN_DataGridMainHeaderRow </title>
</head>
<body>
<form id= "form1 " runat= "server ">
<div>
<asp:DataGrid ID= "DataGrid1 " runat= "server " AutoGenerateColumns= "false " DataKeyField= "ProductID " OnItemDataBound= "DataGrid1_ItemDataBound ">
<Columns>
<asp:BoundColumn DataField= "ProductID " HeaderText= "ProductID "> </asp:BoundColumn>
<asp:BoundColumn DataField= "ProductName " HeaderText= "ProductName " > </asp:BoundColumn>
<asp:BoundColumn DataField= "CategoryID " HeaderText= "CategoryID " > </asp:BoundColumn>
</Columns>
</asp:DataGrid>
</div>
</form>
</body>
</html>
[解决办法]
残月说的有道理.