基于WEB的Grid控件控件显示父子表数据
前台页面代码:
后台代码(Page_Load中调用的方法):
<%@ Page language="c#" Codebehind="DataGridMasterFrom.aspx.cs" AutoEventWireup="false" Inherits="CommonFunction.DataGridMasterFrom" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>DataGridMasterFrom</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<h2>使用DataGrid控件实现以主细表显示数据</h2>
<TABLE cellSpacing="0" cellPadding="0" width="100%" border="0">
<TR>
<TD bgColor="#f0f0f0" width="20%"><B>姓名</B>
</TD>
<TD bgColor="#f0f0f0" width="15%"><B>性别</B>
</TD>
<TD bgColor="#f0f0f0" width="25%"><B>职务</B>
</TD>
<TD bgColor="#f0f0f0" width="20%"><B>电话</B>
</TD>
<TD bgColor="#f0f0f0" width="10%"><B>编辑</B>
</TD>
<TD bgColor="#f0f0f0" width="10%"><B>删除</B>
</TD>
</TR>
</TABLE>
<asp:DataGrid id="dgMaster" runat="server" Width="100%" AutoGenerateColumns="False" ShowHeader="False"
CssClass="90V">
<AlternatingItemStyle HorizontalAlign="Center" BackColor="White"></AlternatingItemStyle>
<ItemStyle HorizontalAlign="Center" BackColor="#FFFFF6"></ItemStyle>
<HeaderStyle HorizontalAlign="Center" BackColor="#F0F0F0"></HeaderStyle>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<TABLE cellSpacing="0" cellPadding="0" width="100%" border="0">
<TR>
<TD bgColor="#F0F0F0" width="20%">
<%# DataBinder.Eval(Container.DataItem, "name") %>
</TD>
<TD bgColor="#F0F0F0" width="15%">
<%# DataBinder.Eval(Container.DataItem, "sex") %>
</TD>
<TD bgColor="#F0F0F0" width="25%">
<%# DataBinder.Eval(Container.DataItem, "duty") %>
</TD>
<TD bgColor="#F0F0F0" width="20%">
<%# DataBinder.Eval(Container.DataItem, "tel") %>
</TD>
<TD bgColor="#F0F0F0" width="10%">
<asp:HyperLink ID="Hyperlink1" Runat=server NavigateUrl='<%# "javascript:edit(\"" + DataBinder.Eval(Container.DataItem, "SubTSUid") + "\")" %>' ImageUrl="image/edit.gif" text="编辑">
</asp:HyperLink>
</TD>
<TD bgColor="#F0F0F0" width="10%">
<asp:ImageButton ID="Imagebutton1" ImageUrl="image/delete.gif" Runat="server" CommandName="Delete" AlternateText='<%# DataBinder.Eval(Container.DataItem, "SubTSUid")%>'>
</asp:ImageButton>
</TD>
</TR>
<TR>
<TD align="center" colSpan="6">
<asp:DataGrid id=dgDetail runat="server" ShowHeader=False Width="100%" AutoGenerateColumns="False" BackColor="White" BorderWidth="0px" BorderColor="#336666" Font-Size="Smaller" DataKeyField="SubTSUid" GridLines="Horizontal" CellPadding="4" DataSource='<%# ((System.Data.DataRowView)Container.DataItem).CreateChildView("tableRelation") %>'>
<AlternatingItemStyle HorizontalAlign="Center" BackColor="White"></AlternatingItemStyle>
<ItemStyle HorizontalAlign="Center" BackColor="#FFFFF6"></ItemStyle>
<HeaderStyle HorizontalAlign="Center" BackColor="#F0F0F0"></HeaderStyle>
<Columns>
<asp:BoundColumn Visible="False" DataField="subcid" ReadOnly="True"></asp:BoundColumn>
<asp:TemplateColumn>
<HeaderStyle Width="250px"></HeaderStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "subcname") %>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Center" ForeColor="White" BackColor="#336666" Mode="NumericPages"></PagerStyle>
</asp:DataGrid>
</TD>
</TR>
</TABLE>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</form>
</body>
</HTML>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>DataGridMasterFrom</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<h2>使用DataGrid控件实现以主细表显示数据</h2>
<TABLE cellSpacing="0" cellPadding="0" width="100%" border="0">
<TR>
<TD bgColor="#f0f0f0" width="20%"><B>姓名</B>
</TD>
<TD bgColor="#f0f0f0" width="15%"><B>性别</B>
</TD>
<TD bgColor="#f0f0f0" width="25%"><B>职务</B>
</TD>
<TD bgColor="#f0f0f0" width="20%"><B>电话</B>
</TD>
<TD bgColor="#f0f0f0" width="10%"><B>编辑</B>
</TD>
<TD bgColor="#f0f0f0" width="10%"><B>删除</B>
</TD>
</TR>
</TABLE>
<asp:DataGrid id="dgMaster" runat="server" Width="100%" AutoGenerateColumns="False" ShowHeader="False"
CssClass="90V">
<AlternatingItemStyle HorizontalAlign="Center" BackColor="White"></AlternatingItemStyle>
<ItemStyle HorizontalAlign="Center" BackColor="#FFFFF6"></ItemStyle>
<HeaderStyle HorizontalAlign="Center" BackColor="#F0F0F0"></HeaderStyle>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<TABLE cellSpacing="0" cellPadding="0" width="100%" border="0">
<TR>
<TD bgColor="#F0F0F0" width="20%">
<%# DataBinder.Eval(Container.DataItem, "name") %>
</TD>
<TD bgColor="#F0F0F0" width="15%">
<%# DataBinder.Eval(Container.DataItem, "sex") %>
</TD>
<TD bgColor="#F0F0F0" width="25%">
<%# DataBinder.Eval(Container.DataItem, "duty") %>
</TD>
<TD bgColor="#F0F0F0" width="20%">
<%# DataBinder.Eval(Container.DataItem, "tel") %>
</TD>
<TD bgColor="#F0F0F0" width="10%">
<asp:HyperLink ID="Hyperlink1" Runat=server NavigateUrl='<%# "javascript:edit(\"" + DataBinder.Eval(Container.DataItem, "SubTSUid") + "\")" %>' ImageUrl="image/edit.gif" text="编辑">
</asp:HyperLink>
</TD>
<TD bgColor="#F0F0F0" width="10%">
<asp:ImageButton ID="Imagebutton1" ImageUrl="image/delete.gif" Runat="server" CommandName="Delete" AlternateText='<%# DataBinder.Eval(Container.DataItem, "SubTSUid")%>'>
</asp:ImageButton>
</TD>
</TR>
<TR>
<TD align="center" colSpan="6">
<asp:DataGrid id=dgDetail runat="server" ShowHeader=False Width="100%" AutoGenerateColumns="False" BackColor="White" BorderWidth="0px" BorderColor="#336666" Font-Size="Smaller" DataKeyField="SubTSUid" GridLines="Horizontal" CellPadding="4" DataSource='<%# ((System.Data.DataRowView)Container.DataItem).CreateChildView("tableRelation") %>'>
<AlternatingItemStyle HorizontalAlign="Center" BackColor="White"></AlternatingItemStyle>
<ItemStyle HorizontalAlign="Center" BackColor="#FFFFF6"></ItemStyle>
<HeaderStyle HorizontalAlign="Center" BackColor="#F0F0F0"></HeaderStyle>
<Columns>
<asp:BoundColumn Visible="False" DataField="subcid" ReadOnly="True"></asp:BoundColumn>
<asp:TemplateColumn>
<HeaderStyle Width="250px"></HeaderStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "subcname") %>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Center" ForeColor="White" BackColor="#336666" Mode="NumericPages"></PagerStyle>
</asp:DataGrid>
</TD>
</TR>
</TABLE>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</form>
</body>
</HTML>
后台代码(Page_Load中调用的方法):
private void load()
{
string strSQL = "select * from TrainingSignUpDetail where TSUid=" + Request.QueryString["TSUid"]
+ " select a.SubTSUid,b.subcid,b.subcname from SignUpRelation a,TrainingSignUpSubClass b where a.subcid=b.subcid";
SqlConnection conn = new SqlConnection(pubValue.GetPubConnString());
//创建数据适配器对象
SqlDataAdapter da = new SqlDataAdapter(strSQL,conn);
//创建DataSet对象
DataSet ds = new DataSet();
try
{
//填充数据集
da.Fill(ds);
//设定表名字
ds.Tables[0].TableName = "person";
ds.Tables[1].TableName = "class";
DataColumn Parent = ds.Tables["person"].Columns["subtsuid"];
DataColumn Child = ds.Tables["class"].Columns["subtsuid"];
DataRelation tableRelation = new DataRelation("tableRelation", Parent, Child, false);
ds.Relations.Add(tableRelation);
//进行数据绑定
this.dgMaster.DataSource = ds.Tables["person"].DefaultView;
this.dgMaster.DataBind();
}
catch(Exception error)
{
Response.Write(error.ToString());
}
}
{
string strSQL = "select * from TrainingSignUpDetail where TSUid=" + Request.QueryString["TSUid"]
+ " select a.SubTSUid,b.subcid,b.subcname from SignUpRelation a,TrainingSignUpSubClass b where a.subcid=b.subcid";
SqlConnection conn = new SqlConnection(pubValue.GetPubConnString());
//创建数据适配器对象
SqlDataAdapter da = new SqlDataAdapter(strSQL,conn);
//创建DataSet对象
DataSet ds = new DataSet();
try
{
//填充数据集
da.Fill(ds);
//设定表名字
ds.Tables[0].TableName = "person";
ds.Tables[1].TableName = "class";
DataColumn Parent = ds.Tables["person"].Columns["subtsuid"];
DataColumn Child = ds.Tables["class"].Columns["subtsuid"];
DataRelation tableRelation = new DataRelation("tableRelation", Parent, Child, false);
ds.Relations.Add(tableRelation);
//进行数据绑定
this.dgMaster.DataSource = ds.Tables["person"].DefaultView;
this.dgMaster.DataBind();
}
catch(Exception error)
{
Response.Write(error.ToString());
}
}