应该从数据库取出的时候就作处理。
然后再databind的时候过滤掉重复的数据。。
然后再databind的时候过滤掉重复的数据。。
解决方案 »
- asp.net中,如何在后台代码中获得服务器端控件的位置???
- 急用!!Datalist
- 请问如何让div的display状态在postback后还能够保持?
- 在frameset中按钮事件怎么触发另一个框架中LightBox效果提示框
- 关于access的菜鸟问题,那位大哥给小弟讲解下。
- 为什么引用存在但是运行时仍提示需要添加引用?
- 想提升一下自己的水平
- 如何提升DAtaSet读取XML的速度(从远程服务器中获取700K左右)
- 关于sql语句中比较时间的问题
- 小问题,我有一个string字符串,是xml文档的内容,我想弄到DataSet中去,怎么弄进去呢ReadXMl方法中string是文件名
- 请教递归在treeview上的应用。
- 请教:为什么Page_Init()没有被执行?
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q308485Vanishing Master/Detail DataGrid
http://www.dotnetjunkies.net/tutorials.aspx?tutorialid=387or play with this simple nested datagrids:<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %><html><body> <h3><font face="Verdana">Nested Grid</font></h3>
<form runat="server">
<ASP:DataGrid id="MyDataGrid" runat="server"
Width="700"
ForeColor="#ff0000"
BackColor=""
BorderColor="black"
ShowFooter="false"
CellPadding=3
CellSpacing="0"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
EnableViewState="false"
OnItemDataBound="Customer_OnItemDataBound"
/></form></body>
</html>
<script language="C#" runat="server"> SqlConnection myConnection; protected void Page_Load(Object sender, EventArgs e)
{
myConnection = new SqlConnection("server=localhost;database=NorthWind;uid=sa;pwd=;");
SqlDataAdapter myCommand = new SqlDataAdapter("select CustomerID, CompanyName ContactName from Customers", myConnection); DataSet ds = new DataSet();
myCommand.Fill(ds, "Customers"); DataView dv = ds.Tables["Customers"].DefaultView;
MyDataGrid.DataSource= dv; MyDataGrid.DataBind(); }
protected void Customer_OnItemDataBound(object sender, DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
String CustomerID = DataBinder.Eval(e.Item.DataItem, "CustomerID").ToString();
DataGrid dg = new DataGrid(); SqlDataAdapter myCommand = new SqlDataAdapter("select OrderID, OrderDate from orders WHERE CustomerID = '" + CustomerID + "'" , myConnection); DataSet ds = new DataSet();
myCommand.Fill(ds, "Orders"); DataView dv = ds.Tables["Orders"].DefaultView;
dg.DataSource= dv;
dg.DataBind(); TableCell cell = new TableCell();
cell.Controls.Add(dg);
e.Item.Cells.Add(cell);
}
}</script>
DataGrid有OnItemDataBound事件可以在创建时绑定子表,
有没有使用repeater的例子?
下面这段代码可以绑定子表,但是我运行时总是说服务器标记错误,错误在这一行:
datasource="<%# ((DataRowView)Container.DataItem)
.Row.GetChildRows("myrelation") %>" runat="server">
能否帮我看看是哪里错了。Nestedrepeater.aspx
<%@ Page Language=C# Inherits="yourprojectname.nestedrepeater" %>
<%@ Import Namespace="System.Data" %>
<html>
<body>
<form runat=server>
<!-- start parent repeater -->
<asp:repeater id="parent" runat="server">
<itemtemplate>
<b><%# DataBinder.Eval(Container.DataItem,"au_id") %></b><br>
<!-- start child repeater -->
<asp:repeater id="child" datasource="<%# ((DataRowView)Container.DataItem)
.Row.GetChildRows("myrelation") %>" runat="server">
<itemtemplate>
<%# DataBinder.Eval(Container.DataItem, "[\"title_id\"]")%><br>
</itemtemplate>
</asp:repeater>
<!-- end child repeater -->
</itemtemplate>
</asp:repeater>
<!-- end parent repeater -->
</form>
</body>
</html>
Nestedrepeater.aspx.cs
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;namespace yourprojectname
{
public class nestedrepeater : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Repeater parent;
public nestedrepeater()
{
Page.Init += new System.EventHandler(Page_Init);
}
public void Page_Load(object sender, EventArgs e)
{
//Create the connection and DataAdapter for the Authors table.
SqlConnection cnn = new SqlConnection("server=(local);database=pubs;uid=sa;pwd=;");
SqlDataAdapter cmd1 = new SqlDataAdapter("select * from authors",cnn); //Create and fill the DataSet.
DataSet ds = new DataSet();
cmd1.Fill(ds,"authors"); //Create a second DataAdapter for the Titles table.
SqlDataAdapter cmd2 = new SqlDataAdapter("select * from titleauthor",cnn);
cmd2.Fill(ds,"titles"); //Create the relation bewtween the Authors and Titles tables.
ds.Relations.Add("myrelation",
ds.Tables["authors"].Columns["au_id"],
ds.Tables["titles"].Columns["au_id"]); //Bind the Authors table to the parent Repeater control, and call DataBind.
parent.DataSource = ds.Tables["authors"];
Page.DataBind(); //Close the connection.
cnn.Close();
}
private void Page_Init(object sender, EventArgs e)
{
InitializeComponent();
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
}
}