In my previous articles, we've explored various aspects of working with GridView in ASP.NET. If you haven't checked them out, here's a quick recap:
- Display Gridview Selected Row Data when clicked: Learn to showcase selected row data dynamically.
- Loading Gridview Records Page Number Wise: Understand how to load GridView records based on selected page numbers.
- Delete GridView Selected Row Records via Button: Explore the process of deleting specific rows with a button click.
- Fix Gridview Header While Scrolling: Make your GridView headers stay visible while scrolling for a better user experience.
- Display GridView Checkbox Selected Row Data in Textbox: Implement the display of selected row data in a textbox using checkboxes.
- Load XML File Into ASP.NET Gridview: Learn how to load XML files seamlessly into your GridView.
Now, let's delve into advanced functionalities like editing, updating, and deleting rows within a GridView. We'll guide you through the CSS styling and C# coding needed to master these features.
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:
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;
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();
}
}
}
}
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;
}
}
GridView Editing, Updating, Deleting, Canceling (Sample Functions)
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...";
}
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...";
}
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();
}
Feel free to adapt and integrate these functionalities into your ASP.NET projects, and take your GridView management skills to the next level. Happy coding!
0 Comments