How can i give table format using asp.net and c# [Resolved]

Posted by Sampath1750 under ASP.NET on 12/16/2011 | Points: 10 | Views : 15221 | Status : [Member] | Replies : 5
Hi,
My Database table has 5 columns Below is my table my data.

SectionId SecName ChapterId ChapterName Duration
1 Sec1 1 Chap1 2 Days
1 Sec1 2 Chap2 2 Days
1 Sec1 3 Chap3 2 Days
2 Sec2 4 Chap4 2 Days
2 Sec2 5 Chap5 2 Days
3 Sec3 6 Chap6 1 Day
3 Sec3 7 Chap7 1 Day
4 Sec4 8 Chap8 1 Day


I want table or gridview or repeater format like below


ChapterName Duration
Sec1
Chap1 2 Days
Chap2 2 Days
Chap3 2 Days
Sec2
Chap4 2 Days
Chap5 2 Days
Sec3
Chap6 1 Day
Chap7 1 Day
Sec4
Chap8 1 Day


How can i display above format dynamically in aspx page using sql server2008

Thanks,
Sampath bejugama




Responses

Posted by: Sksamantaray on: 12/16/2011 [Member] Silver | Points: 50

Up
0
Down

Resolved
You can also use single table and hide duplicate data i.e sec.
Follow this link for example:
http://www.codeproject.com/KB/grid/Unique_Values_GridView.aspx

Thanks,
Sanjay

Sampath1750, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksamantaray on: 12/16/2011 [Member] Silver | Points: 25

Up
0
Down
you can use Master-child grid concept i.e grid within grid
your sp should return 2-tables table-1 will hold sec1,sec2.......
table2 will have chapter details(child data)


Thanks,
Sanjay

Sampath1750, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sampath1750 on: 12/16/2011 [Member] Starter | Points: 25

Up
0
Down
I cant understand clearly, can u give me the sample code

Sampath1750, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksamantaray on: 12/16/2011 [Member] Silver | Points: 25

Up
0
Down
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="SqlDataSource1">
<Columns>

<asp:TemplateField>

<ItemTemplate>
<asp:Label ID="Label1" Visible=false runat="server" Text='<%# Bind("id") %>'></asp:Label>

<asp:Label ID="Label2" runat="server" Text='<%# Bind("sec") %>'></asp:Label>







<br />
<asp:GridView ID="GridView2" DataKeyNames="id" runat="server" DataSourceID="SqlDataSource1">
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:dbRecruitmentConnectionString %>"
SelectCommand="uspChild" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="Label1" Name="id" PropertyName="Text"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>







</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:dbRecruitmentConnectionString %>"
SelectCommand="uspParent" SelectCommandType="StoredProcedure">
</asp:SqlDataSource>

</div>
</form>
</body>
</html>
-----------------------------------------------------------

alter procedure uspParent
as
begin
select ID=1,sec='sec1' into #temp

insert into #temp
select ID=2,sec='sec2'

insert into #temp
select ID=3,sec='sec3'


select * from #temp order by id
drop table #temp

End


alter procedure uspChild
@id int
as
begin
select id=1 ,chapter='Chap1', days=' 2 Days ' into #temp

insert into #temp
select id=1, chapter='Chap2', days=' 5 Days '

insert into #temp
select id=1, chapter='Chap3', days=' 2 Days '

insert into #temp
select id=2, chapter='Chap1', days=' 2 Days '

insert into #temp
select id=2, chapter='Chap2', days=' 2 Days '

insert into #temp
select id=2, chapter='Chap3', days=' 2 Days '
select * from #temp where id=@id order by id
drop table #temp
end
-------------------------------------------------------
Use proper connection string in your web.config

Thanks,
Sanjay

Sampath1750, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksamantaray on: 12/16/2011 [Member] Silver | Points: 25

Up
0
Down
#3rd method
in the rowdatabound of gridview1 find the value of sectionid and then pass this value as parameter to get the chapter details for inner-grid ,finally add the control to parent gridview.

Thanks,
Sanjay

Sampath1750, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response