问题描述
环境:VS.NET2005+C#目标:想利用GridView实现如下图所示表格嵌套(主表为TEST表(有ID,U_NAME,U_AGE三个字段),成绩一栏对应TEST2表(有ID,NAMES,CHN,ENG四个字段))编辑思路:1)创建GridView1,用于接收TEST表数据2)在GridView1中加入<ItemTemplate>模板,并在<ItemTemplate></ItemTemplate>中添加GridView2,用于接收来自TEST2表的查询3)分别创建SQlDataSource1(TEST表的查询)和SqlDataSource2(TEST2表的条件查询)4)在GridView1的protectedvoidGridView1_RowDataBound(objectsender,GridViewRowEventArgse)事件中将GridView1的第2列的值传递给SqlDataSource2的参数,作为参数值代码分别如下,请大家帮我看看....大恩不言谢!TEST2.ASPX代码如下:<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="TEST2.aspx.cs"Inherits="TEST2"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title>UntitledPage</title></head><body><formid="form1"runat="server"><div><asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="DataSource=ADSERVER;InitialCatalog=SZDK;PersistSecurityInfo=True;UserID=sa;Password=62055639"ProviderName="System.Data.SqlClient"SelectCommand="SELECT[ID],[U_NAME],[U_AGE]FROMTEST"></asp:SqlDataSource><asp:SqlDataSourceID="SqlDataSource2"runat="server"ConnectionString="DataSource=ADSERVER;InitialCatalog=SZDK;PersistSecurityInfo=True;UserID=sa;Password=62055639"ProviderName="System.Data.SqlClient"SelectCommand="SELECT[ID],[NAMES],[CHN],[ENG]FROMTEST2WHERE([NAMES]=?)"><SelectParameters><asp:ControlParameterControlID="GridView1"Name="names"PropertyName="SelectedDataKey"Type="String"/></SelectParameters></asp:SqlDataSource><asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="false"DataSourceID="SqlDataSource1"DataKeyNames="ID"AllowPaging="false"AllowSorting="false"PageSize="10"OnRowDataBound="GridView1_RowDataBound"><Columns><asp:BoundFieldDataField="ID"HeaderText="项次"InsertVisible="False"ReadOnly="True"/><asp:BoundFieldDataField="U_NAME"HeaderText="姓名"InsertVisible="False"ReadOnly="True"/><asp:BoundFieldDataField="U_AGE"HeaderText="年龄"InsertVisible="False"ReadOnly="True"/><asp:TemplateFieldHeaderText="成绩"><ItemTemplate><asp:GridViewID="GridView2"runat="server"AutoGenerateColumns="false"DataKeyNames="ID"AllowPaging="false"AllowSorting="false"PageSize="10"><Columns><asp:BoundFieldDataField="ID"HeaderText="项次"InsertVisible="false"ReadOnly="true"/><asp:BoundFieldDataField="NAME"HeaderText="姓名"InsertVisible="false"ReadOnly="true"/><asp:BoundFieldDataField="CHN"HeaderText="语文"InsertVisible="false"ReadOnly="true"/><asp:BoundFieldDataField="ENG"HeaderText="英语"InsertVisible="false"ReadOnly="false"/></Columns></asp:GridView></ItemTemplate></asp:TemplateField></Columns></asp:GridView></div></form></body></html>TEST2.ASPX.CS代码如下:usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Collections;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Web.UI.HtmlControls;publicpartialclassTEST2:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){}protectedvoidGridView1_RowDataBound(objectsender,GridViewRowEventArgse){if(e.Row.RowType==DataControlRowType.DataRow){SqlDataSource2.SelectParameters["names"].DefaultValue=e.Row.Cells[1].Text;GridViewgv=newGridView();gv=(GridView)e.Row.FindControl("GridView2");gv.DataSource=SqlDataSource2;gv.DataBind();}}}
解决方案
解决方案二:
自己顶....帮帮我啊