Showing posts with label Empty Rows Add in GridView. Show all posts
Showing posts with label Empty Rows Add in GridView. Show all posts

Monday, June 16, 2014

Empty Rows Add in GridView

aspx Page

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

<!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">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnadd" runat="server" Text="Add" OnClick="btnadd_Click" />
        <asp:GridView ID="grd" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:TemplateField HeaderText="Firstname">
                    <ItemTemplate>
                        <asp:TextBox ID="txtfirstname" runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Lastname">
                    <ItemTemplate>
                        <asp:TextBox ID="txtlastname" runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Designation">
                    <ItemTemplate>
                        <asp:TextBox ID="txtdesignation" runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Salary">
                    <ItemTemplate>
                        <asp:TextBox ID="txtsalary" runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Mobile number">
                    <ItemTemplate>
                        <asp:TextBox ID="txtmobilenumber" runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Select">
                    <ItemTemplate>
                        <asp:ImageButton ID="imgbtn" runat="server" ImageUrl="~/Images/NARESH PIC.jpg" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:Button ID="btnRegistration" runat="server" Text="Registration" OnClick="btnRegistration_Click" />
    </div>
    </form>
</body>
</html>



---------------------------------------------------------------------------------------------------
ASPC.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;

public partial class EmptyRowsGrid : System.Web.UI.Page
{
    Class1 obj;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session.Clear();
            GridBind();

        }

    }
    protected void btnadd_Click(object sender, EventArgs e)
    {
        if (Session["dt"] == null)
        {
            GridBind();
        }
        else
        {
            DataTable dt2 = (DataTable)Session["dt"];
            dt2.Rows.Add("", "", "", "", "");
            Session["dt"] = dt2;
            grd.DataSource = dt2;
            grd.DataBind();

        }
    }

    private void GridBind()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("firstname", typeof(String));
        dt.Columns.Add("lastname", typeof(String));
        dt.Columns.Add("designation", typeof(String));
        dt.Columns.Add("salary", typeof(String));
        dt.Columns.Add("mobilenumber", typeof(String));
        dt.Rows.Add("", "", "", "", "");
        Session["dt"] = dt;
        grd.DataSource = dt;
        grd.DataBind();
    }
    protected void btnRegistration_Click(object sender, EventArgs e)
    {
        obj = new Class1();
        foreach (GridViewRow grdobj in grd.Rows)
        {
            TextBox txt1 = (TextBox)grdobj.FindControl("txtfirstname");
            TextBox txt2 = (TextBox)grdobj.FindControl("txtlastname");
            TextBox txt3 = (TextBox)grdobj.FindControl("txtdesignation");

            TextBox txt4 = (TextBox)grdobj.FindControl("txtsalary");
            TextBox txt5 = (TextBox)grdobj.FindControl("txtmobilenumber");
            if (txt5.Text == "string")
            {
                Response.Write("values please enter numbers");

            }
            else
            {
                obj.insertvalues(txt1.Text, txt2.Text, txt3.Text, txt4.Text, txt5.Text);
            }
        }
    }

}


------------------------------------------