2
Answers

edit and update details gridview in asp.net without buttons

Now I want that when a user clicks in a cell of the gridview it becomes editable and he can edit the cell values and update. 
I dont wish to use the in-built edit, delete and update buttons of gridview.
How can I do that?
Answers (2)
0
anjali khan

anjali khan

NA 293 35.2k 9y

hi Friend,

this is correct..but i want to be this without using data base..i did everything..now i want
if i click the cell so data is updating in a gv but it is not showing datatable..so i want to take a refresh button..
if i click on this so data should be update on a data table..
i am sending the code like this...

design page

<div style="text-align:center; background-color:SkyBlue; width:100%">
    <h3>GridView</h3>
      <asp:gridview ID="gridview1" runat="server" AutoGenerateColumns="false" AlternatingRowStyle-BackColor="Gray" OnRowDataBound="gridview1_OnRowDataBound" Width="100%">
       <Columns>
        <asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="50" />
         <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="50" />
          <asp:BoundField DataField="City" HeaderText="City" ItemStyle-Width="50" />
       </Columns>
      </asp:gridview>
    </div>

code---


 protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {

                DataTable dt = new DataTable();

                dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("City") });

                dt.Rows.Add(1, "Anamika", "Bangalore");
                dt.Rows.Add(2, "Sunny", "Chennai");
                dt.Rows.Add(3, "Monika", "Bangalore");
                dt.Rows.Add(4, "Jyoti", "Chennai");
                dt.Rows.Add(5, "Radhika", "Jabalpur");
                dt.Rows.Add(6, "Imran", "Jammu");
                dt.Rows.Add(7, "Alok", "Delhi");
                dt.Rows.Add(8, "Amit", "Shamshabad");
                dt.Rows.Add(9, "Neetu", "Bhopal");
                dt.Rows.Add(10, "Jyoti", "Chennai");
                dt.Rows.Add(11, "Radhika", "Vidisha");
                dt.Rows.Add(10, "Pooja", "Pune");

                gridview1.DataSource = dt;
                gridview1.DataBind();

            }
        }
        protected void gridview1_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                for (int i = 0; i < e.Row.Cells.Count; i++)
                {                
                    TextBox txt = new TextBox();
                    txt.Text = e.Row.Cells[i].Text;                
                    e.Row.Cells[i].Text = "";
                    e.Row.Cells[i].Controls.Add(txt);
                }
            }
        }
    }

0
Satyapriya Nayak

Satyapriya Nayak

NA 53k 8m 12y
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Editable_GridView_multiple_records._Default" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="GridView1" runat="server"
                             AllowPaging="True"
                             DataSourceID="SqlDataSource1"
                             AutoGenerateColumns="false">
<Columns>
 
     <asp:TemplateField HeaderText="Select">
 
   
    <ItemTemplate>
    <asp:CheckBox ID="chkSelect" runat="server"
                  AutoPostBack="true"
             OnCheckedChanged="chkSelect_CheckedChanged"/>
    </ItemTemplate>
   
    </asp:TemplateField>
    <asp:BoundField DataField="ID" HeaderText="ID"
                                   SortExpression="ID"/>
                                  
    <asp:TemplateField HeaderText="Name"
                       SortExpression="Name">
    <ItemTemplate>
    <asp:TextBox ID="txtName" runat="server"
                 Text='<%# Bind("Name") %>' ReadOnly="true"
                 ForeColor="Blue" BorderStyle="none"
                 BorderWidth="0px" >
    </asp:TextBox>
    </ItemTemplate>
    </asp:TemplateField>
   
    <asp:TemplateField HeaderText="Location"
                       SortExpression="Location">
    <ItemTemplate>
    <asp:TextBox ID="txtLocation" runat="server"
                 Text='<%# Bind("Location") %>'
                 ReadOnly="true" ForeColor="Blue"
                 BorderStyle="none" BorderWidth="0px">
    </asp:TextBox>
    </ItemTemplate>
    </asp:TemplateField>
</Columns>
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [ID], [Name], [Location] FROM [Details]"
DeleteCommand="DELETE FROM Details WHERE (ID = @ID)"
UpdateCommand="UPDATE [Details] SET [Name] = @Name,
              [Location] = @Location WHERE [ID] = @ID">
        <DeleteParameters>
             <asp:Parameter Name="ID" />
        </DeleteParameters>
        <UpdateParameters>
                <asp:Parameter Name="Name" />
                <asp:Parameter Name="Location" />
                <asp:Parameter Name="ID" />
        </UpdateParameters>
        </asp:SqlDataSource><br />
       
<asp:Button ID="btnUpdate" runat="server"
            OnClick="btnUpdate_Click" Text="Update" /><br />
           

    </div>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
        Text="Check all" />
    <asp:Button ID="Button2" runat="server" onclick="Button2_Click"
        Text="UnCheck all" />
    </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;
using System.Text;
namespace Editable_GridView_multiple_records
{
    public partial class _Default : System.Web.UI.Page
    {
        string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            StringBuilder strSql = new StringBuilder(string.Empty);

            SqlConnection con = new SqlConnection(connStr);
            SqlCommand cmd = new SqlCommand();

            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox chkUpdate = (CheckBox)
                   GridView1.Rows[i].Cells[0].FindControl("chkSelect");
                if (chkUpdate != null)
                {
                    if (chkUpdate.Checked)
                    {

                        string strID = GridView1.Rows[i].Cells[1].Text;
                        string strName = ((TextBox)
                            GridView1.Rows[i].FindControl("txtName")).Text;

                        string strLocation = ((TextBox)
                            GridView1.Rows[i].FindControl("txtLocation")).Text;

                        string strUpdate =
                            "Update Details set Name = '" + strName + "',Location = '" + strLocation + "' WHERE ID ='" + strID + "'";

                        strSql.Append(strUpdate);
                    }
                }
            }
            try
            {
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = strSql.ToString();
                cmd.Connection = con;
                con.Open();
                cmd.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                string errorMsg = "Error in Updation";
                errorMsg += ex.Message;
                throw new Exception(errorMsg);
            }
            finally
            {
                con.Close();
            }
            UncheckAll();
        }
        private void UncheckAll()
        {
            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox chkUncheck = (CheckBox)
                             row.FindControl("chkSelect");
                TextBox txtname = (TextBox)
                               row.FindControl("txtName");
                TextBox txtlocation = (TextBox)
                           row.FindControl("txtLocation");
                chkUncheck.Checked = false;
                txtname.ReadOnly = true;
                txtlocation.ReadOnly = true;
                txtname.ForeColor = System.Drawing.Color.Blue;
                txtlocation.ForeColor = System.Drawing.Color.Blue;
            }
        }
        protected void chkSelect_CheckedChanged
                            (object sender, EventArgs e)
        {
            CheckBox chkTest = (CheckBox)sender;
            GridViewRow grdRow = (GridViewRow)chkTest.NamingContainer;
            TextBox txtname = (TextBox)grdRow.FindControl
                                                ("txtName");
            TextBox txtlocation = (TextBox)grdRow.FindControl
                                              ("txtLocation");
            if (chkTest.Checked)
            {
                txtname.ReadOnly = false;
                txtlocation.ReadOnly = false;
                txtname.ForeColor = System.Drawing.Color.Black;
                txtlocation.ForeColor = System.Drawing.Color.Black;
            }
            else
            {
                txtname.ReadOnly = true;
                txtlocation.ReadOnly = true;
                txtname.ForeColor = System.Drawing.Color.Blue;
                txtlocation.ForeColor = System.Drawing.Color.Blue;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            CheckState(true);
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            CheckState(false);
        }
        private void CheckState(bool p)
        {

            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox chkcheck = (CheckBox)row.FindControl("chkSelect");

                chkcheck.Checked = p;

            }

        }
    }
}