Today, I am going to show you "how to create editable gridview in asp.net" and we will implement editable column ingridview in asp.net using c#. To make editable gridview you need to add <EditItemTemplate> under <asp:TemplateField>.
Also we see step by step insert delete update in gridview using c#. All right let's get start.
First of you need add asp.net webform , here I am going to add a webform with the of emp.aspx.
Step 1: Configure the connectionstring in web.cofig file.
Also Read: Top 10 Web Config Interview Question and Answers
$ads={1}
Step 2: Add asp.net webform with name emp.aspx.
$ads={2}
Step 3: Copy and paste below code in your codebehind page(emp.aspx.cs)
TAG:how to create editable gridview in asp.net
Also we see step by step insert delete update in gridview using c#. All right let's get start.
First of you need add asp.net webform , here I am going to add a webform with the of emp.aspx.
Step 1: Configure the connectionstring in web.cofig file.
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<connectionStrings>
<add name="Mycon" connectionString="initial catalog=jqueryclass; integrated security=true; data source=SANJAY\SQL2012;"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" />
</system.web>
</configuration>
Also Read: Top 10 Web Config Interview Question and Answers
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="emp.aspx.cs" Inherits="emp" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="grdview" runat="server"
OnRowEditing="grdview_RowEditing" DataKeyNames="eid" ShowFooter="true" OnRowUpdating="grdview_RowUpdating"
OnRowDeleting="grdview_RowDeleting" OnRowCancelingEdit="grdview_RowCancelingEdit"
AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Sr No.">
<ItemTemplate>
<%# grdview.Rows.Count + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label runat="server" ID="lblid" Text=' <%#Bind("eid") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField>
<HeaderTemplate>Emp name</HeaderTemplate>
<ItemTemplate><%#Eval("ename") %></ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ftxtname" runat="server"></asp:TextBox>
</FooterTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtnameft" runat="server" Text='<%#Eval("ename") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField>
<HeaderTemplate>Emp Address</HeaderTemplate>
<ItemTemplate>
<%#Eval("address") %>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ftxtaddress" runat="server"></asp:TextBox>
</FooterTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtaddressft" runat="server" Text='<%#Eval("address") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField>
<HeaderTemplate>Emp Age</HeaderTemplate>
<ItemTemplate>
<%#Eval("age") %>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ftxtage" runat="server"></asp:TextBox>
</FooterTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtageft" runat="server" Text='<%#Eval("age") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField>
<HeaderTemplate>Status</HeaderTemplate>
<ItemTemplate>
<%# Eval("IsActive").ToString()=="1"? "Active":"No Active" %>
</ItemTemplate>
<FooterTemplate>
<asp:CheckBox ID="ftxtisactive" runat="server"></asp:CheckBox>
</FooterTemplate>
<EditItemTemplate>
<asp:CheckBox ID="txtisactiveft" runat="server" Text='<%# Eval("IsActive").ToString()=="1"?"Active":"No Active" %>'></asp:CheckBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField>
<HeaderTemplate>operation</HeaderTemplate>
<ItemTemplate>
<asp:Button runat="server" ID="btnedit" Text="edit" CommandName="edit" />
<asp:Button runat="server" ID="btndelete" Text="delete" CommandName="delete" />
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btninsert" runat="server" Text="insert" OnClick="btninsert_Click" />
</FooterTemplate>
<EditItemTemplate>
<asp:Button runat="server" ID="btnupdate" Text="update" CommandName="update" />
<asp:Button runat="server" ID="btncancel" Text="cancel" CommandName="cancel" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>No Record Available Insert Record</EmptyDataTemplate>
</asp:GridView>
</div>
</form>
</body>
</html>
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;
using System.Configuration;
public partial class emp : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Mycon"].ConnectionString);
SqlCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
public void BindData()
{
con.Open();
cmd = new SqlCommand("getday1", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
grdview.DataSource = ds;
grdview.DataBind();
con.Close();
}
public void EditData()
{
con.Open();
cmd = new SqlCommand("getday1", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
grdview.DataSource = ds;
grdview.DataBind();
con.Close();
}
protected void grdview_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("delete from emp where eid='" + Convert.ToInt32(grdview.DataKeys[e.RowIndex].Value.ToString()) + "'", con);
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
Response.Write("<script> alert('Deleted'); </script>");
}
con.Close();
BindData();
}
protected void btninsert_Click(object sender, EventArgs e)
{
int p;
TextBox txt_name = grdview.FooterRow.FindControl("ftxtname") as TextBox;
TextBox txt_adress = grdview.FooterRow.FindControl("ftxtaddress") as TextBox;
TextBox txt_age = grdview.FooterRow.FindControl("ftxtage") as TextBox;
CheckBox txt_isactive = grdview.FooterRow.FindControl("ftxtisactive") as CheckBox;
if (txt_isactive.Checked == true)
{
p = 1;
}
else
{
p = 0;
}
con.Open();
SqlCommand cmd = new SqlCommand("day1emp", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("ename", txt_name.Text);
cmd.Parameters.AddWithValue("address", txt_adress.Text);
cmd.Parameters.AddWithValue("age", txt_age.Text);
cmd.Parameters.AddWithValue("IsActive", p);
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
Response.Write("<script> alert('Inserted'); </script>");
}
con.Close();
BindData();
}
protected void grdview_RowEditing(object sender, GridViewEditEventArgs e)
{
grdview.EditIndex = e.NewEditIndex;
BindData();
}
protected void grdview_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grdview.EditIndex = -1;
BindData();
}
protected void grdview_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label lbldlt = grdview.Rows[e.RowIndex].FindControl("lblid") as Label;
TextBox txt_fname = grdview.Rows[e.RowIndex].FindControl("txtnameft") as TextBox;
TextBox txt_fadress = grdview.Rows[e.RowIndex].FindControl("txtaddressft") as TextBox;
TextBox txt_fage = grdview.Rows[e.RowIndex].FindControl("txtageft") as TextBox;
CheckBox txt_fisactive = grdview.Rows[e.RowIndex].FindControl("txtisactiveft") as CheckBox;
int isactive;
if (txt_fisactive.Checked == true)
{
isactive = 1;
}
else
{
isactive = 0;
}
con.Open();
SqlCommand cmd = new SqlCommand("day1emp_update", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@eid", lbldlt.Text);
cmd.Parameters.AddWithValue("@ename", txt_fname.Text);
cmd.Parameters.AddWithValue("@address", txt_fadress.Text);
cmd.Parameters.AddWithValue("@age", txt_fage.Text);
cmd.Parameters.AddWithValue("@IsActive", isactive);
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
Response.Write("<script> alert('Updated'); </script>");
}
con.Close();
BindData();
}
}
TAG:how to create editable gridview in asp.net
missing database and stored procedures
ReplyDeletePost a Comment