Friday 14 November 2014

How to Insert New Row Using GridView Footer in Asp.Net.


Insert New Row Using  GridView Footer




A GridView Bind to Data Table. But Add GridView Footer  Row Directly.



                         DEMO






Insert Data Using GridView Footer Row 









Html Coding


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

<!DOCTYPE html>


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:gridview ID="Gridview1" runat="server" AutoGenerateColumns="False"
            OnSelectedIndexChanged="Gridview1_SelectedIndexChanged" ShowFooter="True">
             <Columns>
           <asp:TemplateField HeaderText="Action">
             <FooterTemplate>
             <asp:LinkButton ID="LinkButton1" runat="server"
                  CommandName="Select">Insert</asp:LinkButton>
             </FooterTemplate>
             </asp:TemplateField>
             <asp:TemplateField HeaderText="Name">
             <FooterTemplate>
             <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
             </FooterTemplate>
             <ItemTemplate>
             <asp:Label ID="Label1" runat="server" Text='<%# Eval("name") %>'></asp:Label>
             </ItemTemplate>
             </asp:TemplateField>
             <asp:TemplateField HeaderText="Email ID">
             <FooterTemplate>
            <asp:TextBox ID="txtEmailID" runat="server"></asp:TextBox>
            </FooterTemplate>
            <ItemTemplate>
            <asp:Label ID="Label2" runat="server" Text='<%# Eval("email") %>'></asp:Label>
            </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="City">
            <FooterTemplate>
           <asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
          </FooterTemplate>
          <ItemTemplate>
         <asp:Label ID="Label3" runat="server" Text='<%# Eval("city") %>'></asp:Label>
         </ItemTemplate>
        </asp:TemplateField>
             </Columns>
        </asp:gridview>
       
    </div>
    </form>
</body>
</html>




C# Coding



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_Insert : System.Web.UI.Page
{

    SqlConnection con;
    SqlCommand cmd;
    SqlDataAdapter adp;
    SqlDataReader rd;
    DataSet ds;
    string query;

 
    public void dbcon()
    {
        string connn = (System.Configuration.ConfigurationManager.
ConnectionStrings["dbcon"].ToString());
        con = new SqlConnection(connn);
        con.Open();

    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bind11();
        }
    }


    protected void bind11()
    {
        dbcon();
        query = "select * from grid";
        cmd = new SqlCommand(query, con);
        adp = new SqlDataAdapter(cmd);
        ds = new DataSet();
        adp.Fill(ds);
        rd = cmd.ExecuteReader();
        if (ds.Tables[0].Rows.Count > 0)
        {
            Gridview1.DataSource = ds;
            Gridview1.DataBind();
        }
        else
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            Gridview1.DataSource = ds;
            Gridview1.DataBind();
            int columncount = Gridview1.Rows[0].Cells.Count;
            Gridview1.Rows[0].Cells.Clear();
           // GridView1.FooterRow.Cells.Clear();
            Gridview1.Rows[0].Cells.Add(new TableCell());
            Gridview1.Rows[0].Cells[0].ColumnSpan
 = columncount;
            Gridview1.Rows[0].Cells[0].Text = "No Records Found";
        }
    }


    protected void Gridview1_SelectedIndexChanged(object sender, EventArgs e)
    {

        TextBox txtNamee = (TextBox)Gridview1.FooterRow.
FindControl("txtName");
        TextBox txtEmaill = (TextBox)Gridview1.FooterRow.
FindControl("txtEmailID");
        TextBox txtCity = (TextBox)Gridview1.FooterRow.
FindControl("txtCity");

        dbcon();
  query = "insert into grid (name,email,city)values('"+txtNamee.Text+"',
'"+txtEmaill.Text+"','"+txtCity.Text+"')";
        cmd = new SqlCommand(query, con);
        cmd.ExecuteNonQuery();
        con.Close();

        bind11();
    }

}






















































































OUTPUT























                         
                                                   

0 comments:

Post a Comment