Monday, June 16, 2014

Grid Edit And Update Dynamic

ASPX


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

<!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:DropDownList ID="drp" runat="server" AutoPostBack="true" AppendDataBoundItems="true"
            OnSelectedIndexChanged="drp_SelectedIndexChanged">
        </asp:DropDownList>
        <asp:GridView ID="grd" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:TemplateField HeaderText="Id">
                    <ItemTemplate>
                        <asp:Label ID="lblid" runat="server" Text='<%#bind("id") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="First Name">
                    <ItemTemplate>
                        <asp:Label ID="lblfirstname" runat="server" Text='<%#bind("firstname") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Last Name">
                    <ItemTemplate>
                        <asp:Label ID="ibllastname" runat="server" Text='<%#bind("lastname") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Designation">
                    <ItemTemplate>
                        <asp:Label ID="lbldesignation" runat="server" Text='<%#bind("designation") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Salary">
                    <ItemTemplate>
                        <asp:Label ID="lblsalary" runat="server" Text='<%#bind("salary") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Mobile Number">
                    <ItemTemplate>
                        <asp:Label ID="iblmobilenumber" runat="server" Text='<%#bind("mobilenumber") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Select">
                    <ItemTemplate>
                        <asp:TextBox ID="txtbox" runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Edit">
                    <ItemTemplate>
                        <asp:LinkButton ID="linkbtn" runat="server" OnCommand="edit" Text="Edit" CommandArgument='<%#bind("id") %>'
                            CommandName='<%#bind("salary") %>'></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:Button ID="btn" runat="server" Text="update" OnClick="btn_Click" />
    </div>
    </form>
</body>
</html>



-----------------------------------------------------------------------------------------------
ASPX.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 GridDynamic : System.Web.UI.Page
{
    Class1 obj;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            Session.Clear();
            obj = new Class1();
            drp.Items.Clear();
            drp.Items.Add("--Select--");
            drp.DataSource = obj.drpbind();
            drp.DataTextField = "Firstname";
            drp.DataValueField = "id";
            drp.DataBind();

        }
    }
    protected void drp_SelectedIndexChanged(object sender, EventArgs e)
    {
        obj = new Class1();
        if (Convert.ToInt32(drp.SelectedIndex) == 0)
        {
            Response.Write("select one");
        }
        else
        {
            DataSet ds = obj.grdbind(Convert.ToInt32(drp.SelectedValue));

            if (Session["dt"] == null)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("Id", typeof(string));
                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(ds.Tables[0].Rows[0][0].ToString(), ds.Tables[0].Rows[0][1].ToString(), ds.Tables[0].Rows[0][2].ToString(), ds.Tables[0].Rows[0][3].ToString(), ds.Tables[0].Rows[0][4].ToString(), ds.Tables[0].Rows[0][5].ToString());
                Session["dt"] = dt;
                grd.DataSource = dt;
                grd.DataBind();

            }
            else
            {
                DataTable dt2 = (DataTable)Session["dt"];
                dt2.Rows.Add(ds.Tables[0].Rows[0][0].ToString(), ds.Tables[0].Rows[0][1].ToString(), ds.Tables[0].Rows[0][2].ToString(), ds.Tables[0].Rows[0][3].ToString(), ds.Tables[0].Rows[0][4].ToString(), ds.Tables[0].Rows[0][5].ToString());
                Session["dt"] = dt2;
                BindGrid(dt2);


            }



            foreach (GridViewRow objgrd in grd.Rows)
            {

                TextBox txt = (TextBox)objgrd.FindControl("txtbox");
                txt.Visible = false;

            }
        }
    }

    private void BindGrid(DataTable dt2)
    {
        grd.DataSource = dt2;
        grd.DataBind();
    }
    protected void edit(object sender, CommandEventArgs e)
    {

        string id = e.CommandArgument.ToString();
        string salary = e.CommandName.ToString();
        foreach (GridViewRow objgrd in grd.Rows)
        {
            TextBox txt = (TextBox)objgrd.FindControl("txtbox");
            txt.Visible = true;

        }



    }


    protected void btn_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow objgrd in grd.Rows)
        {
            obj = new Class1();
            Label lblid = (Label)objgrd.FindControl("lblid");
            Label lblsalary = (Label)objgrd.FindControl("lblsalary");
            TextBox txt = (TextBox)objgrd.FindControl("txtbox");
            //txt.Visible = true;
            if (txt.Text == "")
            {
                obj.salaryupdate(Convert.ToInt32(lblid.Text), lblsalary.Text);
            }
            else
            {
                obj.salaryupdate(Convert.ToInt32(lblid.Text), txt.Text);

            }
            DataTable dt2 = new DataTable();
            BindGrid(dt2);


        }

    }
}


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