Thursday, 4 April 2013

use of nested DataGrid in asp.net

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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 runat="server">
    <title></title>
    <style type="text/css">
    .a
    {
        list-style:none;
        width:940px;
    }
    .b
    {
        float:left;
        padding-top:3px;
        width:360px;
        text-align:right;
    }
    .c
    {
         float:left;
        padding-top:3px;
        width:20px;
        text-align:center;
         font-weight:900;
    }
     .d
    {
        float:left;
        padding-top:3px;
        width:560px;
        text-align:left;
    }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="sm" runat="server"></asp:ScriptManager>
    <asp:UpdatePanel ID="up" runat="server">
    <ContentTemplate>
      <div align="center">
    <ul class="a">
    <li class="b">EID</li>
     <li class="c">:</li>
      <li class="d"><asp:TextBox ID="tb" runat="server"></asp:TextBox></li>
    </ul>
     <ul class="a">
    <li class="b">NAME</li>
     <li class="c">:</li>
      <li class="d"><asp:TextBox ID="tb1" runat="server" Width="200"></asp:TextBox></li>
    </ul>
     <ul class="a">
    <li class="b">DEPARTMENT NAME</li>
     <li class="c">:</li>
      <li class="d"><asp:DropDownList ID="ddl" runat="server"></asp:DropDownList>&nbsp;<asp:Button
              ID="btn" runat="server" Text="Add More" Width="80" onclick="btn_Click" /></li>
    </ul>
    <ul class="a">
    <li class="b">&nbsp;</li>
     <li class="c">&nbsp;</li>
      <li class="d">&nbsp;<asp:DataGrid ID="dg" runat="server" Width="100%"></asp:DataGrid></li>
    </ul>
    <ul class="a">
    <li class="b">&nbsp;</li>
     <li class="c">&nbsp;</li>
      <li class="d"><asp:Button ID="btn1" runat="server" Text="Save" Width="80"
              onclick="btn1_Click" /></li>
    </ul>
    <ul class="a">
    <li class="b">&nbsp;</li>
     <li class="c">&nbsp;</li>
      <li class="d">&nbsp;<asp:DataGrid ID="dg1" runat="server" Width="100%" AutoGenerateColumns="false">
      <Columns>
      <asp:TemplateColumn>
      <HeaderTemplate>
     <table width="100%">
     <tr><td>Select</td><td>EID</td><td>NAME</td></tr>
      </HeaderTemplate>
      <ItemTemplate>
        <tr><td><asp:LinkButton ID="lbt" runat="server" Text="+" Font-Underline="false"
                CommandArgument='<%#Eval("eid") %>' onclick="lbt_Click"></asp:LinkButton></td><td><%#Eval("eid") %></td><td><%#Eval("name") %></td></tr>
        <tr><td colspan="3"><asp:Panel ID="pn" runat="server" Visible="false">
        <asp:DataGrid ID="dg2" runat="server" Width="100%"></asp:DataGrid>
        </asp:Panel></td></tr>
      </ItemTemplate>
      <FooterTemplate>
      </table>
      </FooterTemplate>
      </asp:TemplateColumn>
      </Columns>
      </asp:DataGrid>
      </li>
    </ul>
    </div>
    </ContentTemplate>
    </asp:UpdatePanel>
 
    </form>
</body>
</html>
c# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseModel;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    #region
    DatabaseEntities1 o = new DatabaseEntities1();
    DataTable dt = new DataTable();
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ddl.DataSource = o.DEPTs.ToList();
            ddl.DataTextField = "dname";
            ddl.DataValueField = "did";
            ddl.DataBind();
            fill();
            ViewState["i"] = 0;
        }
    }
    protected void btn_Click(object sender, EventArgs e)
    {
        dt.Columns.Add("DID", typeof(string));
        dt.Columns.Add("DNAME", typeof(string));
        DataRow dr;
        dr = dt.NewRow();
        dr[0] = ddl.SelectedValue.ToString();
        dr[1] = ddl.SelectedItem.Text;
        dt.Rows.Add(dr);
        foreach (DataGridItem dgi in dg.Items)
        {
            dr = dt.NewRow();
            dr[0] = dgi.Cells[0].Text;
            dr[1] = dgi.Cells[1].Text;
            dt.Rows.Add(dr);
        }
        dg.DataSource = dt;
        dg.DataBind();
    }
    void fill()
    {
        dg1.DataSource = o.EMPs.ToList();
        dg1.DataBind();
    }
    protected void btn1_Click(object sender, EventArgs e)
    {
        o.Connection.Open();
        using (System.Data.Common.DbTransaction dbt = o.Connection.BeginTransaction())
        {
            try
            {
                EMP p = new EMP();
                p.EID = Convert.ToInt32(tb.Text);
                p.NAME = tb1.Text;
                o.EMPs.AddObject(p);
                o.SaveChanges();
                foreach (DataGridItem dgi in dg.Items)
                {
                    empdetail q = new empdetail();
                    q.eid = Convert.ToInt32(tb.Text);
                    q.did = Convert.ToInt32(dgi.Cells[0].Text);
                    o.empdetails.AddObject(q);
                    o.SaveChanges();
                }
                dbt.Commit();
                o.AcceptAllChanges();
                fill();
                ScriptManager.RegisterStartupScript(this, GetType(), "y", "alert('One record saved.')", true);
               
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterClientScriptBlock(this, GetType(), "x", "alert('" + ex.Message.ToString() + "')", true);
                dbt.Rollback();
            }
        }
      
       
    }
    protected void lbt_Click(object sender, EventArgs e)
    {
        LinkButton lb = (LinkButton)sender;
        DataGridItem dgi =(DataGridItem)lb.NamingContainer;
        int i=Convert.ToInt32(lb.CommandArgument);
        if (Convert.ToInt32(ViewState["i"]) == 0)
        {
            ViewState["i"] = 1;
            ((Panel)dgi.FindControl("pn")).Visible = true;
            lb.Text = "-";
        }
        else
        {
            ViewState["i"] = 0;
            ((Panel)dgi.FindControl("pn")).Visible = false;
            lb.Text = "+";
        }
        var m = (from x in o.empdetails
                 join y in o.DEPTs
                 on x.did equals y.DID
                 where x.eid == i
                 select new { y.DNAME }).ToList();
        ((DataGrid)dgi.FindControl("dg2")).DataSource = m;
        ((DataGrid)dgi.FindControl("dg2")).DataBind();
    }
}
page looks like:

No comments:

Post a Comment