Monday, 18 March 2013

How to Create Nested GridView in asp.net


   
What is Nested Gridview :-
                 Gridview Contain more than one other Gridview inside in it. It’s Called a nested Gridview.this type of Facility is provided by asp.net and are very useful to store All Detail in it.
For Example:-
            Suppose we have to Display student personal information and student Education Detail at that time one gridview can not capable of Displaying All information of student like personal and Education because of each contain more Field like Name,Age etc .. in personal information and Degree,Year,Result etc.. in Educational information so that type of information one Gridview can not Display in a proper manner because of screen Width. Due to this problem we are create a Nested Gridview in which parent Gridview contain student personal information and Child Gridview contain Educational Detail so all different type of information are store in proper way.
                So main Advantage of this Example is that we can Display our data in a Proper way so our Website user can easily Understand and we can make a user Friendlly Website through this type of example.


How to Create Nested Gridview in MVC click Here Nested GridView in MVC

to show example of how to send SMS in asp.net click here Send SMS to user in asp.net

How to sorting the Data or row in Gridview Sorting data in Gridview based on cloumn name

to show example of Export Gridview data to PDF in asp.net click here Export gridview data to PDF



showpro1.aspx:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="showpro1.aspx.cs" Inherits="showpro1" EnableEventValidation="false" %>

<!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">
<style type="text/css">
        body
        {
            font-family: Arial;
            font-size: 10pt;
        }
        .Grid td
        {
            background-color: #A1DCF2;
            color: black;
            font-size: 10pt;
            line-height:200%
        }
        .Grid th
        {
            background-color: #3AC0F2;
            color: White;
            font-size: 10pt;
            line-height:200%
        }
        .ChildGrid td
        {
            background-color: #eee !important;
            color: black;
            font-size: 10pt;
            line-height:200%
        }
        .ChildGrid th
        {
            background-color: #6C6C6C !important;
            color: White;
            font-size: 10pt;
            line-height:200%
        }
    </style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    $("[src*=plus]").live("click", function () {
        $(this).closest("tr").after("<tr><td></td><td colspan = '999'>" + $(this).next().html() + "</td></tr>")
        $(this).attr("src", "images/minus.png");
    });
    $("[src*=minus]").live("click", function () {
        $(this).attr("src", "images/plus.png");
        $(this).closest("tr").next().remove();
    });
</script>

    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       <asp:GridView ID="gvCustomers" runat="server" CssClass="Grid"
       
       Font-Size="Large"
        ForeColor="#333333" ShowFooter="True" CellPadding="4" GridLines="None"
            
              AutoGenerateColumns="False"  DataKeyNames="pid"
          
           onrowediting="GridView1_RowEditing" onrowdeleting="GridView1_RowDeleting" onrowdatabound="GridView1_RowDataBound1"
             
              >
           
        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
        <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="White" />
        <Columns>
              <asp:TemplateField HeaderText="">
       <ItemTemplate>
       <img alt = "" style="cursor: pointer" src="images/plus.png" />
      
                     <asp:Panel ID="pnlOrders" runat="server" Style="display: none">
                    <asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="false" CssClass = "ChildGrid">
                        <Columns>
                            <asp:BoundField ItemStyle-Width="150px" DataField="quantity" HeaderText="Quantity" />
                            <asp:BoundField ItemStyle-Width="150px" DataField="rate" HeaderText="Rate" />
                        </Columns>
                    </asp:GridView>
                </asp:Panel>
       </ItemTemplate>
    </asp:TemplateField>
 
     
       <asp:TemplateField HeaderText="ID" Visible="false">
                <ItemTemplate>
                    <asp:Label ID="Label10" runat="server" Text='<%# Eval("pid") %>'></asp:Label>
                </ItemTemplate>
                 <EditItemTemplate>
                     <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("pid") %>'></asp:TextBox>
                 </EditItemTemplate>
             </asp:TemplateField>
          
             <asp:TemplateField HeaderText="name">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("name") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                     <asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("name") %>'></asp:TextBox>
                 </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Description">
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Eval("descp") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                     <asp:TextBox ID="TextBox3" runat="server" Text='<%# Eval("descp") %>'></asp:TextBox>
                 </EditItemTemplate>
                </asp:TemplateField>
             <asp:TemplateField HeaderText="Total">
                <ItemTemplate>
                    <asp:Label ID="Label5" runat="server"  Text='<%# Eval("total") %>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                <asp:Label ID="lblgrand" runat="server"></asp:Label>
                </FooterTemplate>
               
                </asp:TemplateField>
          
           <asp:TemplateField>
                <ItemTemplate>
            <asp:LinkButton ID="LinkButton2" runat="server" CommandName="Delete"  >Delete</asp:LinkButton>
            </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
            <ItemTemplate>
             <asp:LinkButton ID="LinkButton1" runat="server"  CommandName="Edit" >Edit</asp:LinkButton>
            </ItemTemplate>
            </asp:TemplateField>
        
            </Columns>
          
    </asp:GridView>

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


showpro1.aspx.cs


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 showpro1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      //  ClientScript.RegisterClientScriptBlock(this.GetType(), "key", "<script> alert('added successfully');</script>");
        grid();

    }
    public void grid()
    {
        DataTable ds = new DataTable();

        string con = @"Data Source=SQLDB;Initial Catalog=Demo;User ID=Demod;Password=Demo1@";
       // "select k_sale.id,k_order.mid,k_order.cid,k_order.pro_image,k_order.pr_name,k_order.prize,k_order.quantity,k_order.status,k_order.total,kclient.ccname,kclient.cstate,kclient.ccity,kclient.caddress from k_order INNER JOIN kclient  ON k_order.cid=kclient.id  INNER JOIN k_sale  ON k_sale.oid=k_order.id where k_order.mid ='" + Session["login"].ToString() + "' and k_order.status ='" + z + "'";
        string q = "SELECT hk_product.name, hk_product.descp, hk_product.pid,SUM(hk_rproduct.total) AS total  FROM hk_product INNER JOIN hk_rproduct ON hk_product.pid = hk_rproduct.rpid GROUP BY hk_product.pid, hk_product.name, hk_product.descp";

        SqlConnection conn = new SqlConnection(con);

        SqlCommand cmd = new SqlCommand(q, conn);



        SqlDataAdapter sa = new SqlDataAdapter();
        conn.Open();
        cmd.ExecuteNonQuery();
        sa.SelectCommand = cmd;
        sa.Fill(ds);
        gvCustomers.DataSource = ds;
        gvCustomers.DataBind();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {



        Label l = (Label)gvCustomers.Rows[e.NewEditIndex].FindControl("Label10");
        Response.Redirect("prorelgrid.aspx?id=" + l.Text);




    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

        int id = Int32.Parse(gvCustomers.DataKeys[e.RowIndex].Value.ToString());
        string con = @"Data Source=SQLDB;Initial Catalog=Demo;User ID=Demod;Password=Demo1@";
        //Menu inm = (Menu)this.Master.FindControl("Menu1");

      //string q1="DELETE FROM  hk_product t1 INNER JOIN hk_rproduct t2 ON ( t1.pid = t2.rpid )";
        string q1 = "delete  from hk_product where pid = " + id;

       // string q2 = "delete from hk_rproduct where rpid = " + id;
        SqlConnection cn = new SqlConnection(con);

        SqlCommand cmd = new SqlCommand(q1, cn);
       // SqlCommand cmd1 = new SqlCommand(q2, cn);
        cn.Open();

       // cmd1.ExecuteNonQuery();
        cmd.ExecuteNonQuery();
        gvCustomers.EditIndex = -1;
        grid();




    }
   


    protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            Label l = (Label)e.Row.FindControl("lblgrand");
            string con = @"Data Source=SQLDB;Initial Catalog=Demo;User ID=Demod;Password=Demo1@";
            // "select k_sale.id,k_order.mid,k_order.cid,k_order.pro_image,k_order.pr_name,k_order.prize,k_order.quantity,k_order.status,k_order.total,kclient.ccname,kclient.cstate,kclient.ccity,kclient.caddress from k_order INNER JOIN kclient  ON k_order.cid=kclient.id  INNER JOIN k_sale  ON k_sale.oid=k_order.id where k_order.mid ='" + Session["login"].ToString() + "' and k_order.status ='" + z + "'";
            string q = "select sum(total) from hk_rproduct";

            SqlConnection conn = new SqlConnection(con);

            SqlCommand cmd = new SqlCommand(q, conn);
            conn.Open();
            l.Text = cmd.ExecuteScalar().ToString();

        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string customerId = gvCustomers.DataKeys[e.Row.RowIndex].Value.ToString();
            GridView gvOrders = e.Row.FindControl("gvOrders") as GridView;
            string con = @"Data Source=SQLDB;Initial Catalog=Demo;User ID=Demod;Password=Demo1@";
            // "select k_sale.id,k_order.mid,k_order.cid,k_order.pro_image,k_order.pr_name,k_order.prize,k_order.quantity,k_order.status,k_order.total,kclient.ccname,kclient.cstate,kclient.ccity,kclient.caddress from k_order INNER JOIN kclient  ON k_order.cid=kclient.id  INNER JOIN k_sale  ON k_sale.oid=k_order.id where k_order.mid ='" + Session["login"].ToString() + "' and k_order.status ='" + z + "'";
            string q = "select quantity,rate from hk_rproduct";

            SqlConnection conn = new SqlConnection(con);
            DataTable dt = new DataTable();
            SqlDataAdapter sa = new SqlDataAdapter();
            SqlCommand cmd = new SqlCommand(q, conn);
            conn.Open();
            cmd.ExecuteNonQuery();
            sa.SelectCommand = cmd;
            sa.Fill(dt);
            gvOrders.DataSource = dt;
            gvOrders.DataBind();
        }

    }
}

0 comments:

Post a Comment