Showing posts with label Grid Add 10 records at a Time using sessions and Onrowdatabound. Show all posts
Showing posts with label Grid Add 10 records at a Time using sessions and Onrowdatabound. Show all posts

Sunday, June 29, 2014

Grid Add 10 records at a Time using sessions and Onrowdatabound



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

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="grdout" AutoGenerateColumns="false" runat="server" OnRowDataBound="nnn" DataKeyNames="Id">
      <Columns>
        <asp:TemplateField HeaderText="ID">
          <ItemTemplate>
            <asp:TextBox ID="txt1" runat="server"></asp:TextBox>
          </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Name">
          <ItemTemplate>
            <asp:TextBox ID="txt2" runat="server"></asp:TextBox>
          </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Role">
          <ItemTemplate>
            <asp:TextBox ID="txt3" runat="server"></asp:TextBox>
          </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Gender">
          <ItemTemplate>
           <asp:DropDownList ID="drp" runat="server"></asp:DropDownList>
          </ItemTemplate>
        </asp:TemplateField>

      </Columns>
   
    </asp:GridView>
   
        <br />
        <asp:Button ID="Button1" runat="server" Text="ADD NEW ROW"
            onclick="Button1_Click" />
   
    </div>
    </form>
</body>
</html>

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

public partial class Grid : System.Web.UI.Page
{
    //int count;
    protected void Page_Load(object sender, EventArgs e)
    {
        int count = 10;
        if (!IsPostBack)
        {
            Session.Clear();
            for (int i = 0; i <= count; i++)
            {

               
                naresh();

            }
         }
       
      
    }
    protected void nnn(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
           
            var ddl = (DropDownList)e.Row.FindControl("drp");
          
            SqlConnection con = new SqlConnection("Data Source=KCLINK-45-PC\\SQLEXPRESS;Initial Catalog=naresh;Integrated Security=True");
            SqlCommand cmd = new SqlCommand("select * from gender");
            cmd.Connection = con;
            con.Open();
            SqlDataAdapter dap = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            dap.Fill(ds);
            ddl.DataSource = ds;
            ddl.DataTextField = "gender";
            ddl.DataValueField = "id";
            ddl.DataBind();
            ddl.Items.Insert(0, new ListItem("--Select--", "0"));

          
        }

    
       
    }
    private void naresh()
    {
        DataTable dt = new DataTable();

        dt.Columns.Add("Id", typeof(string));
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("Role", typeof(string));
        dt.Columns.Add("Gender", typeof(string));
        if (Session["dt"] == null)
        {

            dt.Rows.Add("", "", "", "");
            Session["dt"] = dt;
            grdout.DataSource = dt;
            grdout.DataBind();

        }
        else
        {
            DataTable dt2 = (DataTable)Session["dt"];
            dt2.Rows.Add("", "", "", "");
            Session["dt"] = dt2;
            grdout.DataSource = dt2;
            grdout.DataBind();
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DataTable dtt = new DataTable();

        dtt.Columns.Add("Id", typeof(string));
        dtt.Columns.Add("Name", typeof(string));
        dtt.Columns.Add("Role", typeof(string));
        dtt.Columns.Add("Gender", typeof(string));
        if (Session["dt"] == null)
        {

            dtt.Rows.Add("", "", "", "");
            Session["dt"] = dtt;
            grdout.DataSource = dtt;
            grdout.DataBind();

        }
        else
        {
            DataTable dt3 = (DataTable)Session["dt"];
            dt3.Rows.Add("", "", "", "");
            Session["dt"] = dt3;
            grdout.DataSource = dt3;
            grdout.DataBind();
        }
    }

}