如何在gridviewParent中展开(按钮)gridviewChild [英] How to expand(Button) gridviewChild in gridviewParent

查看:60
本文介绍了如何在gridviewParent中展开(按钮)gridviewChild的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我有一个webform包含gridview1(dgvParent)内的gridview2(dgvChild),
在gridview1(dgvParent)列中,例如CustID,客户名称,地址,联系人,
展开(模板字段(项目模板)中的按钮),客户订单明细(Gridview 2nd在这里)现在我想要在gridview1中按Expand按钮的同时,
gridview2(客户订单明细)记录应与行中的展开按钮相对应显示.展开后,该特定按钮应变为折叠,然后
将特定的按钮更改折叠成expand.

我正在寻找的主要概念,如何在按扩展按钮的同时显示记录(在dgvChild中).任何建议都应先感谢您.获取代码要好得多.

提醒两个dgv从不同的不同表中填充,但也要提醒第二个表中的dgvParent CustId.给我最简单的建议.


在此先感谢........

我的代码如下所示...


Hi All,
I have a webform contain gridview2(dgvChild) inside gridview1(dgvParent),
In gridview1(dgvParent) Columns like CustID,Customer Name,Address,Contact,
Expand(Button in Template field(Item Template)),Customer order details(Gridview 2nd is here) Now i want that while pressing Expand button in gridview1,
gridview2(Customer order details) record should display correponding to expand button in the rows. After expand that particular button should change into collapse and after
collapse that particular button change into expand.

Main concept i am looking for, how to show records(In dgvChild) while pressing expand button. Any suggestion appreciate thanks in advance.Getting code is much better.

Reminder both dgv filled from different different table but dgvParent CustId in 2nd table also. provide me easiest suggestion.


Thanks in Advance........

My code is shown below......


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class GridInsideGrid : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindGridParent1();
    }
    private void BindGridParent1()
    {
        
        SqlConnection con = new SqlConnection(@"server=ITP4\SQLEXPRESS;database=Namit1;UId=sa;Pwd=pass");
        con.Open();
        SqlCommand cmd = new SqlCommand("Select * from Customer2", con);
        SqlDataAdapter dA = new SqlDataAdapter();
        dA.SelectCommand = cmd;
        DataSet ds = new DataSet();
        dA.Fill(ds);
        dgvParent.DataSource = ds.Tables[0].DefaultView;
        dgvParent.DataBind();
        con.Close();
        BindGridChild1();
    }
    private void BindGridChild1()
    {
        
        SqlConnection con = new SqlConnection(@"server=ITP4\SQLEXPRESS;database=Namit1;UId=sa;Pwd=pass");
        con.Open();

        for (int j = 0; j < dgvParent.Rows.Count; j++)
        {

            GridView dgv = (GridView)dgvParent.Rows[j].Cells[4].FindControl("dgvChild");
            string ABCD = dgvParent.Rows[j].Cells[0].Text;
            SqlCommand cmd = new SqlCommand("Select * from Order1 where CustID='" + ABCD + "'", con);
            SqlDataAdapter dA = new SqlDataAdapter();
            dA.SelectCommand = cmd;
            DataSet ds = new DataSet();
            dA.Fill(ds);
            dgv.DataSource = ds.Tables[0].DefaultView;
            dgv.DataBind();
        }
        con.Close();
    }
    protected void btnShow_Click(object sender, EventArgs e)
    {

    }
}

推荐答案


本示例将指导您.
Hi ,
This Example Will Guide you .
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
 <script type="text/javascript">
     function SwitchImages(obj) {
         var div = document.getElementById(obj);
         var img = document.getElementById('img' + obj);

         if (div.style.display == "none") {
             div.style.display = "inline";
             img.src = "images/minus.png";
         } else {
             div.style.display = "none";
             img.src = "images/plus.png";
         }
     }
 </script>
 <style type="text/css">
 .GDiv
{
    display:none;
    position:relative;
    font-size:16px;
    color:#434343;
    width:100%;
    font-weight:bold;
    border:1px solid #ccc;
    min-height:30px;

}

 </style>
</head>
<body >
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

            CellPadding="4" ForeColor="#333333" GridLines="None"

            onrowdatabound="GridView1_RowDataBound">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <Columns>
                <asp:TemplateField HeaderText="Order ID">
                    <ItemTemplate>
                        <asp:Label ID="lblID" runat="server" Text="<%# Bind('orderID') %>"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="order amount">
                    <ItemTemplate>
                        <asp:Label ID="lblAmount" runat="server" Text="<%# Bind('OrderAmount') %>"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="date ">
                    <ItemTemplate>
                        <asp:Label ID="lblDate" runat="server" Text="<%# Bind('date') %>"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>

                              <a href="javascript:SwitchImages('div<%# Eval("orderID")%>');">
                            <img id="imgdiv<%# Eval("orderID")%>" alt="" border="0" src="images/plus.png" />
                        </a>
                      </td></tr>
                        <tr>
                            <td colspan="100%">
                                <div id="div<%# Eval("orderID") %>" class="GDiv">

                                                 <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" Width="95%">
                                <Columns>
                <asp:TemplateField HeaderText="OrderDetailsiD ">
                                        <ItemTemplate>
                                            <asp:Label ID="Label1" runat="server" Text="<%# Bind('OrderDetailsiD') %>"></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="order id">
                                        <ItemTemplate>
                                            <asp:Label ID="Label2" runat="server" Text="<%# Bind('orderID') %>"></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Product ID">
                                        <ItemTemplate>
                                            <asp:Label ID="Label3" runat="server" Text="<%# Bind('productID') %>"></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="QTY">
                                        <ItemTemplate >
                                            <asp:Label ID="Label4" runat="server" Text="<%# Bind('Qty') %>"></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>
                                            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                            </asp:GridView>
                            </div>
                        </td>
                        </tr>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>



背后的代码:



Code Behind:

public partial class Default43 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bind();
        }

    }
    void bind()
    {
        using (
              SqlConnection con =
                  new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString))
        {

            SqlCommand cmd = new SqlCommand("select  *  from dbo.Orders ", con);
            DataTable dt = new DataTable();
            SqlDataAdapter adpt = new SqlDataAdapter(cmd);
            adpt.Fill(dt);
            GridView1.DataSource = null;
            GridView1.DataSource = dt;
            GridView1.DataBind();

        }
    }
    DataTable BindSecGrid(int id)
    {
        using (
                  SqlConnection con =
                      new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString))
        {

            string Query = "select * from dbo.ODetails where orderID =" + id;
            SqlCommand cmd = new SqlCommand(Query, con);
            DataTable dt = new DataTable();
            SqlDataAdapter adpt = new SqlDataAdapter(cmd);
            adpt.Fill(dt);

            return dt;
        }
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            GridView gv = ((GridView)e.Row.FindControl("GridView2"));
            if ((Label)e.Row.FindControl("lblID") is Label)
            {
                int id = Convert.ToInt32(((Label)e.Row.FindControl("lblID")).Text);
                gv.DataSource = BindSecGrid(id);
                gv.DataBind();
            }
            
        }
    }
}



表格脚本



Script for Tables

CREATE TABLE [dbo].[ODetails](
    [OrderDetailsiD] [int] IDENTITY(1,1) NOT NULL,
    [orderID] [int] NULL,
    [productID] [int] NULL,
    [Qty] [int] NULL,
 CONSTRAINT [PK_ODetails] PRIMARY KEY CLUSTERED
(
    [OrderDetailsiD] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]





CREATE TABLE [dbo].[Orders](
    [orderID] [int] IDENTITY(1,1) NOT NULL,
    [OrderAmount] [int] NULL,
    [date] [date] NULL,
 CONSTRAINT [PK_Orders] PRIMARY KEY CLUSTERED
(
    [orderID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]



最好的问候
米特瓦里(M.Mitwalli)



Best Regards
M.Mitwalli


这篇关于如何在gridviewParent中展开(按钮)gridviewChild的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆