Monday, September 5, 2011

Storing Data in Dynamic DataTable And Display in GridView


Source Code:

In Form Tag Write below source code



 <table align="center" class="style1">
        <tr>
            <td>
                &nbsp;</td>
            <td class="style2">
                F.Name</td>
            <td class="style3">
                <asp:TextBox ID="txtFName" runat="server"></asp:TextBox>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td class="style2">
                L.Name</td>
            <td class="style3">
                <asp:TextBox ID="txtLName" runat="server"></asp:TextBox>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td class="style2">
                Address</td>
            <td class="style3">
                <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td class="style2">
                Phone</td>
            <td class="style3">
                <asp:TextBox ID="txtPhone" runat="server"></asp:TextBox>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td class="style2">
                &nbsp;</td>
            <td class="style3">
                <asp:Button ID="btnSave" runat="server" onclick="btnSave_Click" Text="Save" />
            </td>
            <td>
                <asp:Label ID="lblmsg" runat="server"></asp:Label>
            </td>
        </tr>
        <tr>
            <td align="center" colspan="4">
                <asp:GridView ID="GridView1" runat="server" BackColor="LightGoldenrodYellow"
                    BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black"
                    GridLines="None">
                    <FooterStyle BackColor="Tan" />
                    <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue"
                        HorizontalAlign="Center" />
                    <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
                    <HeaderStyle BackColor="Tan" Font-Bold="True" />
                    <AlternatingRowStyle BackColor="PaleGoldenrod" />
                </asp:GridView>
            </td>
        </tr>
    </table>


C# Code:



using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class DynamicDataTable : System.Web.UI.Page
{
    static int i=1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dt = new DataTable();
            DataColumn dtcol = new DataColumn("Id", typeof(System.Int32));
            dtcol.AutoIncrement = true;
            dt.Columns.Add(dtcol);
            dtcol = new DataColumn("FName", typeof(System.String));
            dt.Columns.Add(dtcol);
            dtcol = new DataColumn("LName", typeof(System.String));
            dt.Columns.Add(dtcol);
            dtcol = new DataColumn("Address", typeof(System.String));
            dt.Columns.Add(dtcol);
            dtcol = new DataColumn("Phone", typeof(System.String));
            dt.Columns.Add(dtcol);
            int j = form1.Controls.Count;
            Session["datatable"] = dt;
            if (Session["dt"] != null)
            {
                GridView1.DataSource = Session["dt"].ToString();
                GridView1.DataBind();
            }
            else
            {
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        DataTable dtt = (DataTable)Session["datatable"];
        DataRow dr = dtt.NewRow();
        dr[0] = i++;
        dr[1] = txtFName.Text;
        dr[2] = txtLName.Text;
        dr[3] = txtAddress.Text;
        dr[4] = txtPhone.Text;
        dtt.Rows.Add(dr);
        Session["id"] = dtt.ToString();
        GridView1.DataSource = dtt;
        GridView1.DataBind();
        lblmsg.Text = "Inserted Successfully..." + dr[0].ToString() + "Records";
    }
}

No comments:

Post a Comment