GridView中嵌套GridView

一:

//delete   Demo   ,   use   pubs
//aspx
<%@   Page   Language= "C# "   AutoEventWireup= "true "   CodeBehind= "GridViewDemo.aspx.cs "   Inherits= "GridViewNesting "   %>
<!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   id= "Head1 "   runat= "server ">
        <title> GridViewNesting </title>
        <style>  
        </style>  
</head>
<body>
        <form   id= "form1 "   runat= "server ">
        <asp:GridView   ID= "GridView1 "   runat= "server "   AutoGenerateColumns= "False "   DataKeyNames= "au_id "   OnRowDeleting= "GridView1_RowDeleting "   >
        <Columns>
                        <asp:CommandField   ShowEditButton= "True "   />
                        <asp:CommandField   ShowDeleteButton= "True "   />
                <asp:TemplateField   HeaderText= "au_id "   >
                        <ItemTemplate>
                        <asp:Label   id=Label1   runat= "server "   Text= ' <%#   Eval( "au_id ")%> '>
                        </asp:Label>
                        </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField     HeaderText= "au_lname ">
                        <ItemTemplate>
                        <asp:Label   id=Label2   runat= "server "   Text= ' <%#   Eval( "au_lname ")%> '>
                        </asp:Label>
                        </ItemTemplate>
                </asp:TemplateField>
                        <asp:TemplateField     HeaderText= "titleDetail ">
                        <ItemTemplate>
                                        <asp:GridView   ID= "GridView2 "   runat= "server "   AutoGenerateColumns= "False "   DataKeyNames= "au_id,title_id "   DataSource= ' <%#   GetTitleID(Eval( "au_id ").ToString())   %> '   OnRowDeleting= "GridView2_OnRowDeleting ">
                                        <Columns>
                                                                <asp:CommandField   ShowEditButton= "True "   />
                        <asp:CommandField   ShowDeleteButton= "True "   />
                                                <asp:TemplateField   HeaderText= "title_id "   >
                                                        <ItemTemplate>
                                                        <asp:Label   id=Label1   runat= "server "   Text= ' <%#   Eval( "title_id ")%> '>
                                                        </asp:Label>
                                                        </ItemTemplate>
                                                </asp:TemplateField>
                                                <asp:TemplateField     HeaderText= "au_ord ">
                                                        <ItemTemplate>
                                                        <asp:Label   id=Label2   runat= "server "   Text= ' <%#   Eval( "au_ord ")%> '>
                                                        </asp:Label>
                                                        </ItemTemplate>
                                                </asp:TemplateField>
                                                </Columns>
                                </asp:GridView>
                        </ItemTemplate>
                </asp:TemplateField>
                </Columns>
</asp:GridView>
</form>
</body>
</html>

//aspx.cs
using   System;
using   System.Data;
using   System.Configuration;
using   System.Collections;
using   System.Web;
using   System.Web.Security;
using   System.Web.UI;
using   System.Web.UI.WebControls;
using   System.Web.UI.WebControls.WebParts;
using   System.Web.UI.HtmlControls;
using   System.Data.SqlClient;

public   partial   class   GridViewNesting   :   System.Web.UI.Page
{
        private   void   BindGrid()
        {
                SqlConnection   cn   =   new   SqlConnection(@ "server=./SQLExpress;uid=sa;pwd=password;database=pubs ");
                SqlDataAdapter   da   =   new   SqlDataAdapter( "select   *   from   authors ",   cn);
                DataSet   ds   =   new   DataSet();
                cn.Open();
                da.Fill(ds);
                cn.Close();
                GridView1.DataSource   =   ds;
                GridView1.DataBind();
        }

        public   DataView   GetTitleID(string   au_id)
        {
                SqlConnection   cn   =   new   SqlConnection(@ "server=./SQLExpress;uid=sa;pwd=password;database=pubs ");
                SqlDataAdapter   da   =   new   SqlDataAdapter( "select   au_id,   title_id,   au_ord   from   titleauthor   where   au_id   =   @au_id ",   cn);
                da.SelectCommand.Parameters.Add( "@au_id ",   SqlDbType.VarChar,   11).Value   =   au_id;
                DataSet   ds   =   new   DataSet();
                cn.Open();
                da.Fill(ds);
                cn.Close();
                return   ds.Tables[0].DefaultView;
        }

        private   void   Page_Load(object   sender,   System.EventArgs   e)
        {
                if   (!IsPostBack)
                {
                        BindGrid();
                }
        }

        protected   void   GridView1_RowDeleting(object   sender,   GridViewDeleteEventArgs   e)
        {
                SqlConnection   cn   =   new   SqlConnection(@ "server=./SQLExpress;uid=sa;pwd=password;database=pubs ");
                SqlCommand   cmd   =   new   SqlCommand();
                cmd.CommandText   =   "delete   from   titleauthor   where   au_id   =   @au_id ";
                cmd.Connection   =   cn;
                cmd.Parameters.Add( "@au_id ",   SqlDbType.VarChar).Value   =   GridView1.DataKeys[e.RowIndex].Value.ToString();
                cn.Open();
                cmd.ExecuteNonQuery();
                cmd.CommandText   =   "delete   from   authors   where   au_id   =   @au_id ";
                cmd.ExecuteNonQuery();
                cn.Close();
                BindGrid();
        }

        protected   void   GridView2_OnRowDeleting(object   sender,   GridViewDeleteEventArgs   e)
        {
                GridView   GridView2   =   (GridView)sender;
                SqlConnection   cn   =   new   SqlConnection(@ "server=./SQLExpress;uid=sa;pwd=password;database=pubs ");
                SqlCommand   cmd   =   new   SqlCommand( "delete   from   titleauthor   where   au_id   =   @au_id   and   title_id   =   @title_id ",   cn);
                cmd.Parameters.Add( "@au_id ",   SqlDbType.VarChar).Value   =   GridView2.DataKeys[e.RowIndex].Values[0].ToString();
                cmd.Parameters.Add( "@title_id ",   SqlDbType.VarChar).Value   =   GridView2.DataKeys[e.RowIndex].Values[1].ToString();
                cn.Open();
                cmd.ExecuteNonQuery();
                cn.Close();
                BindGrid();
        }
}

 

 

二:

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" GridLines="None"
                                    ShowHeader="False" OnRowDataBound="GridView2_RowDataBound">
               <Columns>
                     <asp:TemplateField>
                             <ItemTemplate>
                                 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true">
                                 </asp:GridView>
                              </ItemTemplate>
                      </asp:TemplateField>
              </Columns>
  </asp:GridView>
//aspx.cs页面
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            GridView Grd = e.Row.Cells[0].FindControl("GridView1") as GridView;
            Grd.DataSource = 数据集;
            Grd.DataBind();
        }      
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值