Create an application in C# to insert, delete, and Edit the data using Gridview control.
Program:
.aspx file
<%@ Page Language="C#" Debug="true" AutoEventWireup="true" CodeFile="grid.aspx.cs"
Inherits="Account_grid" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvdatabase" runat="server" AllowPaging="True" AutoGenerateColumns="False"
DataKeyNames="id" ShowFooter="True" OnPageIndexChanging="gvdatabase_PageIndexChanging"
OnRowCancelingEdit="gvdatabase_RowCancelingEdit" OnRowDeleting="gvdatabase_RowDeleting"
OnRowEditing="gvdatabase_RowEditing" OnRowUpdating="gvdatabase_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblid" runat="server" Text='<%#Eval("ID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblid" runat="server" Text='<%#Eval("ID") %>'></asp:Label>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtIid" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblname" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtname" runat="server" Text='<%#Eval("Name") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtIname" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Designation">
<ItemTemplate>
<asp:Label ID="lblsurname" runat="server" Text='<%#Eval("Designation") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtdesignation" runat="server" Text='<%#Eval("Designation") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtIsurname" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Command">
<ItemTemplate>
<asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" />
<asp:Button ID="Delete" runat="server" Text="Delete" CommandName="Delete" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="btnUpdate" runat="server" Text="Update" CommandName="Update" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" />
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="btninsert" runat="server" Text="Insert" OnClick="btninsert_Click" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Program:
.aspx file
<%@ Page Language="C#" Debug="true" AutoEventWireup="true" CodeFile="grid.aspx.cs"
Inherits="Account_grid" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvdatabase" runat="server" AllowPaging="True" AutoGenerateColumns="False"
DataKeyNames="id" ShowFooter="True" OnPageIndexChanging="gvdatabase_PageIndexChanging"
OnRowCancelingEdit="gvdatabase_RowCancelingEdit" OnRowDeleting="gvdatabase_RowDeleting"
OnRowEditing="gvdatabase_RowEditing" OnRowUpdating="gvdatabase_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblid" runat="server" Text='<%#Eval("ID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblid" runat="server" Text='<%#Eval("ID") %>'></asp:Label>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtIid" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblname" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtname" runat="server" Text='<%#Eval("Name") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtIname" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Designation">
<ItemTemplate>
<asp:Label ID="lblsurname" runat="server" Text='<%#Eval("Designation") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtdesignation" runat="server" Text='<%#Eval("Designation") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtIsurname" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Command">
<ItemTemplate>
<asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" />
<asp:Button ID="Delete" runat="server" Text="Delete" CommandName="Delete" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="btnUpdate" runat="server" Text="Update" CommandName="Update" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" />
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="btninsert" runat="server" Text="Insert" OnClick="btninsert_Click" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</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;
using System.Data.SqlClient;
using System.Configuration;
public partial class Account_grid : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString1"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind_Grid();
}
}
private void bind_Grid()
{
con.Open();
SqlCommand cmd = new SqlCommand("Select * from Table_1", con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
cmd.ExecuteNonQuery();
da.Fill(ds);
gvdatabase.DataSource = ds;
gvdatabase.DataBind();
con.Close();
}
protected void gvdatabase_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvdatabase.PageIndex = e.NewPageIndex;
bind_Grid();
}
protected void gvdatabase_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvdatabase.EditIndex = -1;
bind_Grid();
}
protected void gvdatabase_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id;
id = Convert.ToInt32(gvdatabase.DataKeys[e.RowIndex].Value);
con.Open();
SqlCommand cmd = new SqlCommand("Delete from Table_1 where ID=" + id + "", con);
cmd.ExecuteNonQuery();
con.Close();
bind_Grid();
}
protected void gvdatabase_RowEditing(object sender, GridViewEditEventArgs e)
{
gvdatabase.EditIndex = e.NewEditIndex;
bind_Grid();
}
protected void gvdatabase_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row;
row = gvdatabase.Rows[e.RowIndex];
Label lblid = row.FindControl("lblid") as Label;
TextBox txtname = row.FindControl("txtname") as TextBox;
TextBox txtdesignation = row.FindControl("txtdesignation") as TextBox;
con.Open();
SqlCommand cmd = new SqlCommand("update Table_1 set Name='" + txtname.Text + "',Designation= '" + txtdesignation.Text + "' where ID='" + lblid.Text + "'", con);
cmd.ExecuteNonQuery();
con.Close();
gvdatabase.EditIndex = -1;
bind_Grid();
}
protected void btninsert_Click(object sender, EventArgs e)
{
TextBox txtid = gvdatabase.FooterRow.FindControl("txtIid") as TextBox;
TextBox txtname = gvdatabase.FooterRow.FindControl("txtIname") as TextBox;
TextBox txtdesignation = gvdatabase.FooterRow.FindControl("txtIsurname") as TextBox; con.Open();
SqlCommand cmd = new SqlCommand("insert into Table_1 values('" + txtid.Text + "','" + txtname.Text + "','" + txtdesignation.Text + "')", con);
cmd.ExecuteNonQuery();
con.Close();
bind_Grid();
}
}
Demo:
Create an application in C# to insert, delete, and Edit the data using Gridview control. |
No comments:
Post a Comment