集团一个数据集与嵌套Repeater [英] Group one DataSet with Nested Repeater
问题描述
假设我有一些像这样的书数据:
Suppose I have some book data like this:
和我有这样一个查询 SELECT * FROM BookData code>它输出它上面的格式。
And I have a query like SELECT * FROM BookData
which outputs it in the above format.
我想使用嵌套Repeater控件输出数据的HTML <表>
,这将是这个样子,与作者进行分组数据的结果:
I would like to use nested repeater controls to output the data in an html <table>
that will look something like this, with the data results grouped by Author:
到目前为止,我有我的asp.net web表单code看起来像这样:
So far I have my asp.net web form code looking like this:
<table>
<asp:Repeater ID="RepeaterInner" runat="server">
<ItemTemplate>
<tr>
<td><asp:TextBox Text='<%# Eval("Book") %>' /></td>
<td><asp:TextBox Text='<%# Eval("Author") %>' /></td>
<td><asp:TextBox Text='<%# Eval("PublishDate") %>' /></td>
<td><asp:TextBox Text='<%# Eval("Pages") %>' /></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
和背后code这是我的:
and my behind code this:
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = GetData();
RepeaterInner.DataSource = ds;
RepeaterInner.DataBind();
}
private DataSet GetData()
{
string CS = ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
using (SqlCommand cmd = new SqlCommand("spGetData"))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
con.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
return ds;
}
}
}
有关最终产品,我希望它看起来是这样的:
For the final product I want it to look like this:
<table>
<asp:Repeater ID="RepeaterOuter" runat="server">
<ItemTemplate>
<tr>
<td><asp:TextBox Text='<%# Eval("Author") %>' /></td>
</tr>
<asp:Repeater ID="RepeaterInner" runat="server">
<ItemTemplate>
<tr>
<td><asp:TextBox Text='<%# Eval("Book") %>' /></td>
<td><asp:TextBox Text='<%# Eval("PublishDate") %>' /></td>
<td><asp:TextBox Text='<%# Eval("Pages") %>' /></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
</table>
但我对如何做才能使上面的发生真的很困惑。可以把它与我的一种完成的DataSet
上面从我的一个存储的过程中产生?什么我需要什么,如果?每个转发器的的ItemDataBound
事件处理程序
But I am really confused on what to do to make the above happen. Can it be done with my one DataSet
above generated from my one stored procedure? What do I need to do for each Repeater's ItemDataBound
event handler if anything?
感谢您。
推荐答案
您的存储过程看起来只返回一个数据表。所以GetData方法也许应该返回一个数据表
不是的DataSet
Your stored procedure looks to only return one table of data. So the GetData method should probably return a DataTable
not a DataSet
private DataTable GetData()
{
DataTable dt = new DataTable();
using (SqlConnection c = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString))
{
c.Open();
using (SqlCommand cmd = new SqlCommand("spGetData", c))
{
cmd.CommandType = CommandType.StoredProcedure;
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(dt);
}
}
}
return dt;
}
Servy的解决方案,帮助我理解 IGrouping
更好。建设更多的自己的想法:
你的的Page_Load
方法:
Servy's solution helped me understand IGrouping
better. Building more on his idea:
Your Page_Load
method:
protected void Page_Load(object sender, EventArgs e)
{
DataTable dataTbl = this.GetData();
//Cast the Rows collection so we can use LINQ
IEnumerable<dynamic> data = dt.Rows.Cast<DataRow>()
// Group the DataRows by the column ["Author"]
.GroupBy<DataRow, String>(d => Convert.ToString(d["Author"]))
// GroupBy returns an IEnumerable<IGrouping<TKey, TSource>>
// in this case we have a String (Authors Name) and
// DataRows with the book information. From these IGroupings
// we want to return a dynamic class that has properties:
// - Author (string it is the .Key in the IGrouping)
// - Books (Collection of another dynamic class that has Book properties)
.Select<IGrouping<String, DataRow>, dynamic>(grp => {
return new {
Author = grp.Key,
// grp right now is a collection of DataRows
// make each row into a dynamic class that has
// book properties so that Eval("PropName")
// plays nice
Books = grp.Select<DataRow, dynamic>(row => {
return new {
Book = Convert.ToString(row["Book"]),
PublishDate = Convert.ToString(row["PublishDate"]),
Pages = Convert.ToString(row["Pages"])
};
})
};
});
RepeaterOuter.DataSource = data;
RepeaterOuter.DataBind();
}
页code:
<table>
<asp:Repeater ID="RepeaterOuter" runat="server">
<ItemTemplate>
<tr>
<td><asp:TextBox runat="server" Text='<%# Eval("Author") %>' /></td>
</tr>
<asp:Repeater ID="RepeaterInner" runat="server" DataSource='<%# Eval("Books") %>'>
<ItemTemplate>
<tr>
<td><asp:TextBox runat="server" Text='<%# Eval("Book") %>' /></td>
<td><asp:TextBox runat="server" Text='<%# Eval("PublishDate") %>' /></td>
<td><asp:TextBox runat="server" Text='<%# Eval("Pages") %>' /></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
</table>
这篇关于集团一个数据集与嵌套Repeater的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!