Tuesday, 3 January 2012

Multiple insert from gridview

 Source File Coading
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="datatable.aspx.cs" Inherits="datatable" %>

<!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:TextBox ID="TextBox2" runat="server"
            style="position:absolute; top: 102px; left: 75px;"></asp:TextBox>
        <asp:TextBox ID="TextBox3" runat="server"
            style="position:absolute; top: 141px; left: 77px;"></asp:TextBox>
   
    </div>

        &nbsp;
    <asp:Label ID="Label1" runat="server" Text="Roll"
        style="position:absolute; top: 60px; left: 30px;"></asp:Label>
    <asp:Label ID="Label2" runat="server" Text="Name"
        style="position:absolute; top: 98px; left: 28px;"></asp:Label>
    <asp:Label ID="Label3" runat="server" Text="Address"
        style="position:absolute; top: 137px; left: 12px; height: 18px;"></asp:Label>
   
        <asp:TextBox ID="TextBox1" runat="server"
        style="position:absolute; top: 61px; left: 79px; height: 22px; width: 128px;"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Add" 
        style="position:absolute; top: 169px; left: 96px;"
        onclick="Button1_Click"/>
    <asp:Button ID="Button2" runat="server" Text="Save"
        style="position:absolute; top: 357px; left: 108px;" />
    <asp:GridView ID="GridView1" runat="server"
        style="position:absolute; top: 213px; left: 47px;"
        AutoGenerateColumns="False" onrowcommand="GridView1_RowCommand">

        <Columns>
       <asp:BoundField HeaderText="Roll No" DataField="roll" />
        <asp:BoundField HeaderText="Name" DataField="Name" />
         <asp:BoundField HeaderText="Address" DataField="Address" />
         <asp:TemplateField>
         <ItemTemplate>
            <asp:LinkButton ID="LinkEdite" CommandName="Edi" runat="server">Edit</asp:LinkButton>
            </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
             <ItemTemplate>
            <asp:LinkButton ID="LinkDelete" CommandName="Del" runat="server">Delete</asp:LinkButton>
            </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    </form>
</body>
</html>

C# Coading

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 datatable : System.Web.UI.Page
{
    DataTable dt;
    static int flag = 0;
    static int rowno = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            flag = 0;
            rowno = 0;
            dt = new DataTable();
            dt.Columns.Add("roll",typeof(string));
            dt.Columns.Add("Name",typeof(string));
            dt.Columns.Add("Address",typeof(string));
            ViewState["tabledata"] = dt;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        dt = new DataTable();
        dt=(DataTable)ViewState["tabledata"];
        DataRow dr;
        if (flag == 0)
        {
            dr = dt.NewRow();
            dr[0] = TextBox1.Text;
            dr[1] = TextBox2.Text;
            dr[2] = TextBox3.Text;
            dt.Rows.Add(dr);
        }
        else if (flag == 1)
        {
            dt.Rows[rowno][0] = TextBox1.Text;
            dt.Rows[rowno][1] = TextBox2.Text;
            dt.Rows[rowno][2] = TextBox3.Text;
        }
        ViewState["tabledata"] = dt;
        fillGrid();
    }
  
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        LinkButton lk = (LinkButton)e.CommandSource;
        GridViewRow gr = (GridViewRow)lk.NamingContainer;
        dt = new DataTable();
        dt=(DataTable)ViewState["tabledata"];
        if (e.CommandName == "Edi")
        {
            rowno = gr.RowIndex;
            TextBox1.Text = GridView1.Rows[rowno].Cells[0].Text;
            TextBox2.Text = GridView1.Rows[rowno].Cells[1].Text;
            TextBox3.Text = GridView1.Rows[rowno].Cells[2].Text;
            flag = 1;
        }
        if (e.CommandName == "Del")
        {
            dt.Rows.RemoveAt(rowno);
            ViewState["tabledata"] = dt;
            fillGrid();
        }
    }
    protected void fillGrid()
    {
        dt = (DataTable)ViewState["tabledata"];
        if (dt.Rows.Count > 0)
        {
            GridView1.DataSource = dt;
            GridView1.DataBind();

        }
        else
        {
            GridView1.Dispose();
            GridView1.DataBind();
        }
        flag = 0;
        rowno = 0;
    }
}

Page Design


No comments:

Post a Comment