In my Previous Articles about Gridview Related :
- Display Gridview Selected Row Data when we click on row
- Loading Gridview Records from Selecting Page Number's Wise
- Delete GridView Selected Row Record through Button
- Fix Gridview Header While Scrolling in Asp.net
- Gridview Checkbox Selected Row Data Display in Textbox
- Load XML File Into ASP.NET Gridview

Css Code
<style type="text/css"> #GridView1 { background:#fafafa; } #GridView1 .HeaderStyle { background:#0066cc; color:#ffffff; padding:7px 10px 7px 10px; } #GridView1 th { padding:10px 15px 10px 15px; } #GridView1 tr { padding:5px 10px 5px 10px; text-align:left; } #GridView1 tr:hover { background:#6699cc; color:#ffffff; padding:5px 10px 5px 10px; text-align:left; } .input-disabled { background-color:#EBEBE4; border:1px solid #ABADB3; padding:2px 1px; } </style>
C# Coding
C# Coding : Namespace
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Configuration; using System.Data; using System.Data.SqlClient;
C# Coding : Page Load
namespace WebApplication1.Gridviews { public partial class WebForm1 : System.Web.UI.Page { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["mcon"].ConnectionString); SqlDataAdapter da = new SqlDataAdapter(); DataSet ds; protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) { FillGridView(); } } } }
C# Coding : Bing Gridview
public void FillGridView() { try { ds = new DataSet(); da = new SqlDataAdapter("select Book_id, Book_name, Book_author, Book_Publisher_name from Book", con); da.Fill(ds); if (ds.Tables[0].Rows.Count > 0) { GridView1.DataSource = ds; GridView1.DataBind(); } } catch { throw; } }
C# Coding : Gridview Editiong
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { GridViewRow gdrow = GridView1.Rows[e.RowIndex]; Label lblbookID = (Label)gdrow.FindControl("lblbookid"); TextBox txtbookname = (TextBox)gdrow.Cells[2].Controls[0]; TextBox txtbookauthor = (TextBox)gdrow.Cells[3].Controls[0]; TextBox txtbookpubname = (TextBox)gdrow.Cells[4].Controls[0]; SqlCommand cmd = new SqlCommand("Update Book Set Book_name='" + txtbookname.Text + "', Book_author='" + txtbookauthor.Text + "',Book_Publisher_name='" + txtbookpubname.Text + "' where Book_id='" + lblbookID.Text + "'", con); con.Open(); cmd.ExecuteNonQuery(); con.Close(); GridView1.EditIndex = -1; FillGridView(); lbldisplay.Text = " Book ID : " + lblbookID.Text + " Updated Successfully..."; }
C# Coding : Gridview Updating
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { GridViewRow gdrow = GridView1.Rows[e.RowIndex]; Label lblbookID = (Label)gdrow.FindControl("lblbookid"); TextBox txtbookname = (TextBox)gdrow.Cells[2].Controls[0]; TextBox txtbookauthor = (TextBox)gdrow.Cells[3].Controls[0]; TextBox txtbookpubname = (TextBox)gdrow.Cells[4].Controls[0]; SqlCommand cmd = new SqlCommand("Update Book Set Book_name='" + txtbookname.Text + "', Book_author='" + txtbookauthor.Text + "',Book_Publisher_name='" + txtbookpubname.Text + "' where Book_id='" + lblbookID.Text + "'", con); con.Open(); cmd.ExecuteNonQuery(); con.Close(); GridView1.EditIndex = -1; FillGridView(); lbldisplay.Text = " Book ID : " + lblbookID.Text + " Updated Successfully..."; }
C# Coding : Gridview Deleting
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { GridViewRow gdrow = GridView1.Rows[e.RowIndex]; Label lblbookID = (Label)gdrow.FindControl("lblbookid"); SqlCommand cmd = new SqlCommand("Delete Book where Book_Id='" + lblbookID.Text + "'", con); con.Open(); cmd.ExecuteScalar(); con.Close(); GridView1.EditIndex = -1; FillGridView(); lbldisplay.Text = " Book ID : " + lblbookID.Text + " Deleted Successfully..."; }
C# Coding : Gridview Canceling
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; FillGridView(); }
0 Komentar untuk "Gridview Edit, Update, Delete Button in Asp.net C#"