Friday, 24 February 2012

Gridview Edit Using Cells And Rows in Asp.Net

create a database name log and run the following script
USE [log]
GO
/****** Object:  Table [dbo].[login]    Script Date: 02/24/2012 13:11:01 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Student_S](
 [id] [int] IDENTITY(1,1) NOT NULL,
 [Sid] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 [Firstname] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 [LastName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 [Phone] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 [Department] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 
 CONSTRAINT [PK_Student_S] PRIMARY KEY CLUSTERED 
(
 [Sid] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="gridedit.aspx.cs" Inherits="gridedit" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <h2 style="color: blue; font-style: normal">
        Gridview edit in asp.net
    </h2>
    <div style="text-align: center">
        <asp:GridView ID="grdedit" runat="server" AutoGenerateColumns="False" OnRowDataBound="grdedit_RowDataBound"
            OnRowCommand="grdedit_RowCommand" Width="249px">
            <Columns>
                <asp:TemplateField HeaderText="User ID">
                    <ItemTemplate>
                        <asp:Label ID="lbluserid" Text='<%#Eval("id") %>' runat="server"></asp:Label><asp:TextBox
                            ID="txtuserid" Visible="false" runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Password">
                    <ItemTemplate>
                        <asp:Label ID="lblpswd" Text='<%#Eval("Password") %>' runat="server"></asp:Label><asp:TextBox
                            ID="txtpswd" Visible="false" runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="email">
                    <ItemTemplate>
                        <asp:Label ID="lblcount" Text='<%#Eval("Email") %>' runat="server"></asp:Label><asp:TextBox
                            ID="txtcount" Visible="false" runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Button Text="EDIT" ID="btnedit" Visible="true" runat="server" CommandName="EditOne" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class gridedit : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Employee"].ToString());

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

        all();
    }
    protected void bind()
    {
        SqlDataAdapter da = new SqlDataAdapter("select * from login ", con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        grdedit.DataSource = ds;
        grdedit.DataBind();
    }
    protected void all()
    {
        for (int i = 0; i < grdedit.Rows.Count; i++)
        {
            TextBox t1 = (TextBox)grdedit.Rows[i].Cells[0].FindControl("txtuserid");
            t1.Visible = false;
            TextBox t2 = (TextBox)grdedit.Rows[i].Cells[1].FindControl("txtpswd");
            t2.Visible = false;
            TextBox t3 = (TextBox)grdedit.Rows[i].Cells[2].FindControl("txtcount");
            t3.Visible = false;
            Label l1 = (Label)grdedit.Rows[i].Cells[0].FindControl("lbluserid");
            l1.Visible = true;
            Label l2 = (Label)grdedit.Rows[i].Cells[1].FindControl("lblpswd");
            l2.Visible = true;
            Label l3 = (Label)grdedit.Rows[i].Cells[2].FindControl("lblcount");
            l3.Visible = true;
        }
    }
    int k = 0;
    protected void grdedit_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (grdedit.Rows.Count > 0)
        {
            Label l = (Label)grdedit.Rows[k].Cells[0].FindControl("lbluserid");

            if (l.Text == "santosh")
            {
                Button b = (Button)grdedit.Rows[k].Cells[3].FindControl("btnedit");

                b.Visible = true;
            }

            k++;

        }

        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            Button btn = (Button)e.Row.FindControl("btnedit");
            if (btn != null)
            {
                btn.CommandArgument = e.Row.RowIndex.ToString();
            }

        }
    }
    protected void grdedit_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "EditOne")
        {
            int rowIndex = int.Parse(e.CommandArgument.ToString());
            TextBox t1 = (TextBox)grdedit.Rows[rowIndex].Cells[0].FindControl("txtuserid");
            t1.Visible = true;
            TextBox t2 = (TextBox)grdedit.Rows[rowIndex].Cells[1].FindControl("txtpswd");
            t2.Visible = true;
            TextBox t3 = (TextBox)grdedit.Rows[rowIndex].Cells[2].FindControl("txtcount");
            t3.Visible = true;
            Label l1 = (Label)grdedit.Rows[rowIndex].Cells[0].FindControl("lbluserid");
            l1.Visible = false;
            Label l2 = (Label)grdedit.Rows[rowIndex].Cells[1].FindControl("lblpswd");
            l2.Visible = false;
            Label l3 = (Label)grdedit.Rows[rowIndex].Cells[2].FindControl("lblcount");
            l3.Visible = false;
            t1.Text = l1.Text;
            t2.Text = l2.Text;
            t3.Text = l3.Text;

        }

    }
}


No comments:

Post a Comment