From: Richard Kirk on 4 Feb 2010 12:06 Hi there, I'm quite new to asp.net, but know exactly how to do this using classic ASP, but would like some pointers for aspnet2. I've got data being returned from an SP which looks like this: HeaderItem ChildItem A A1 A A2 A A3 B B1 etc I want to display the data like this A - This is the header record A1 A2 A3 B - Header 2 B1 etc. In classic ASP I'd write out my header row html when HeaderItem changes value and then write the Child Rows. As I've only got one DataSource to work with it doesn't look like I can use embedded Repeaters (I can't get A and then get all the children). Any suggestions as to which controls I should be using, and some pointers on what I should be doing in my RowDataBound, etc. Any help (or example links) most welcome. Thanks.
From: Alexey Smirnov on 4 Feb 2010 16:42 On Feb 4, 6:06 pm, "Richard Kirk" <desirepath.REM...(a)ntlworld.com> wrote: > Hi there, > > I'm quite new to asp.net, but know exactly how to do this using classic ASP, > but would like some pointers for aspnet2. > > I've got data being returned from an SP which looks like this: > > HeaderItem ChildItem > A A1 > A A2 > A A3 > B B1 > etc > > I want to display the data like this > > A - This is the header record > A1 > A2 > A3 > B - Header 2 > B1 > etc. > > In classic ASP I'd write out my header row html when HeaderItem changes > value and then write the Child Rows. > > As I've only got one DataSource to work with it doesn't look like I can use > embedded Repeaters (I can't get A and then get all the children). > > Any suggestions as to which controls I should be using, and some pointers on > what I should be doing in my RowDataBound, etc. > > Any help (or example links) most welcome. > > Thanks. You can use Repeater and a nested List/GridView Control. The Repeater will be used to show A..B and the GridView would list A1..A2..A3 etc. In this case you would also need your stored procedure to return 2 datasets: 1st will be used to return data for the Repeater (A..B) and 2nd to return data for the GridView. Just a simple example: string sql = "SELECT * FROM Headers; SELECT * FROM Items" DataSet ds = ... // Attach the relationship to the dataSet ds.Relations.Add(new DataRelation("ItemsRelation", ds.Tables[0].Columns["HeaderId"], ds.Tables[1].Columns["HeaderId"])); Repeater1.DataSource = ds.Tables[0]; Repeater1.DataBind(); This would bind your Repeater Control to 1st table with A..B Then add OnItemDataBound binding to your Repeater Control markup <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound"> and in the codebehind protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { DataRowView drv = e.Item.DataItem as DataRowView; GridView GridView1 = e.Item.FindControl("GridView1") as GridView; GridView1.DataSource = drv.CreateChildView("ItemsRelation"); GridView1.DataBind(); } } You can find many examples in the internet, just search for "nested gridview" or something like this. Hope this helps
From: Richard Kirk on 4 Feb 2010 17:18 Thanks for the reply. Yes I'd looked at nested repeaters, but it didn't really solve my problem. It's probably explained better by this article I found http://weblogs.sqlteam.com/jeffs/archive/2007/11/02/parent-child-datatable-nested-repeaters.aspx The temporary table returned from my stored procedure is a very heavy duty duplicate locator and I would like to use it as it is without adding extra processing time. It seems like a quite common and simple thing to want to do, and I've been doing this in various languages for the last 20 years, i.e. 1. Read record. 2. Has the value in the first field changed? If so write a header record. 3. Write detail line. Does this really require restructuring the SPs and SQL in ASP.NET? I feel like a novice and that I'm missing something really obvious. Thanks again.
From: Alexey Smirnov on 4 Feb 2010 17:42 On Feb 4, 11:18 pm, "Richard Kirk" <desirepath.REM...(a)ntlworld.com> wrote: > Thanks for the reply. > > Yes I'd looked at nested repeaters, but it didn't really solve my problem.. > > It's probably explained better by this article I foundhttp://weblogs.sqlteam.com/jeffs/archive/2007/11/02/parent-child-data... > > The temporary table returned from my stored procedure is a very heavy duty > duplicate locator and I would like to use it as it is without adding extra > processing time. > > It seems like a quite common and simple thing to want to do, and I've been > doing this in various languages for the last 20 years, i.e. > > 1. Read record. > > 2. Has the value in the first field changed? If so write a header record. > > 3. Write detail line. > > Does this really require restructuring the SPs and SQL in ASP.NET? I feel > like a novice and that I'm missing something really obvious. > > Thanks again. Hi Richard, sure, you can do it as in classic ASP. DataTable tbl = ... string x = ""; for (int i = 0; i < tbl.Rows.Count; i++) { DataRow myRow = tbl.Rows[i]; string MyValue = myRow["datafieldname"]; if (MyValue == x) { .... } else { Response.Write(MyValue); x = MyValue; } } but in this case you can't use Repeater. The only way I see is to enumerate all rows (like above) and create new dataset with HeaderItems. Then this new dataset could be used to bind 1st repeater and the original dataset will be used for a nested control.
From: Alexey Smirnov on 4 Feb 2010 17:51 On Feb 4, 11:18 pm, "Richard Kirk" <desirepath.REM...(a)ntlworld.com> wrote: > Thanks for the reply. > > Yes I'd looked at nested repeaters, but it didn't really solve my problem.. > > It's probably explained better by this article I foundhttp://weblogs.sqlteam.com/jeffs/archive/2007/11/02/parent-child-data... > Yes, what he does in his example is exactly what I said, he created an additional table, and a relation to the first (original) table.
|
Next
|
Last
Pages: 1 2 Prev: Populate image from db Next: Paging using Gridview and ObjectDataSource |