In this Article I am going to explain how to update the gridview records
here is my Home.aspx page
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Home.aspx.cs" Inherits="TutionFeeApp.Home" %>
<!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="gvd_Fee_Display" runat="server" ShowFooter="true" DataKeyNames="id" AllowPaging="True" OnRowDataBound="gvd_Fee_Display_RowDataBound" AutoGenerateColumns="false" OnPageIndexChanging="gvd_Fee_Display_PageIndexChanging" OnRowEditing="gvd_Fee_Display_RowEditing" OnRowUpdating="gvd_Fee_Display_RowUpdating" OnRowCancelingEdit="gvd_Fee_Display_RowCancelingEdit">
<Columns>
<asp:BoundField HeaderText="ID" DataField="ID" ReadOnly="true" SortExpression="ID" />
<asp:BoundField DataField="StudentName" HeaderText="StudentName" SortExpression="StudentName"/>
<asp:BoundField DataField="Standard" HeaderText="Standard" SortExpression="Standard" />
<asp:BoundField DataField="MobileNo" HeaderText="MobileNo" SortExpression="MobileNo" />
<asp:BoundField DataField="DateOfJoin" HeaderText="DateOfJoin" SortExpression="DateOfJoin" FooterText="Total" />
<asp:BoundField DataField="Fee" DataFormatString="{0:c}" HeaderText="Fee"/>
<asp:CommandField ShowEditButton="true" />
</Columns>
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
</asp:GridView>
<%--<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TutionFeeConnectionString %>" SelectCommand="SELECT [ID], [StudentName], [Fee], [MobileNo], [DateOfJoin], [Standard] FROM [tbl_Tution_Fee_Details]"></asp:SqlDataSource>--%>
</div>
</form>
</body>
</html>
Here is my Home.aspx.cs page
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;
namespace TutionFeeApp
{
public partial class Home : System.Web.UI.Page
{
public string _Constr = ConfigurationManager.ConnectionStrings["Tution_Constr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Display();
}
}
protected void Display()
{
SqlConnection con = new SqlConnection(_Constr);
SqlCommand cmd = new SqlCommand("Select * from tbl_Tution_Fee_Details", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
if (ds.Tables[0].Rows.Count > 0)
{
gvd_Fee_Display.DataSource = ds;
gvd_Fee_Display.DataBind();
}
else
{
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
gvd_Fee_Display.DataSource = ds;
gvd_Fee_Display.DataBind();
int columncount = gvd_Fee_Display.Rows[0].Cells.Count;
gvd_Fee_Display.Rows[0].Cells.Clear();
gvd_Fee_Display.Rows[0].Cells.Add(new TableCell());
gvd_Fee_Display.Rows[0].Cells[0].ColumnSpan = columncount;
gvd_Fee_Display.Rows[0].Cells[0].Text = "No Records Found";
}
// SQL query that gets total of product sales where category id = 1
string SqlQuery = @"SELECT SUM(Fee) AS TotalAmount
FROM tbl_Tution_Fee_Details";
// Declare and open a connection to database
SqlConnection conn = new SqlConnection(_Constr);
conn.Open();
// Creates SqlCommand object
SqlCommand comm = new SqlCommand(SqlQuery, conn);
// Gets total sales
decimal TotalSales = Convert.ToDecimal(comm.ExecuteScalar());
// Close connection
conn.Close();
conn.Dispose();
comm.Dispose();
// Adds formatted output to GridView footer
gvd_Fee_Display.Columns[5].FooterText = String.Format("{0:c}", TotalSales);
}
private decimal TotalAmount = (decimal)0.0;
protected void gvd_Fee_Display_RowDataBound(object sender, GridViewRowEventArgs e)
{
// check row type
if (e.Row.RowType == DataControlRowType.DataRow)
// if row type is DataRow, add ProductSales value to TotalSales
TotalAmount += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Fee"));
else if (e.Row.RowType == DataControlRowType.Footer)
// If row type is footer, show calculated total value
// Since this example uses sales in dollars, I formatted output as currency
e.Row.Cells[5].Text = String.Format("{0:c}", TotalAmount);
}
protected void gvd_Fee_Display_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvd_Fee_Display.PageIndex = e.NewPageIndex;
Display();
}
protected void gvd_Fee_Display_RowEditing(object sender, GridViewEditEventArgs e)
{
gvd_Fee_Display.EditIndex = e.NewEditIndex;
Display();
}
protected void gvd_Fee_Display_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
SqlConnection con = new SqlConnection(_Constr);
int userid = Convert.ToInt32(gvd_Fee_Display.DataKeys[e.RowIndex].Value.ToString());
GridViewRow row = (GridViewRow)gvd_Fee_Display.Rows[e.RowIndex];
TextBox txtmobile = (TextBox)row.Cells[3].Controls[0];
TextBox txtfee = (TextBox)row.Cells[5].Controls[0];
gvd_Fee_Display.EditIndex = -1;
con.Open();
SqlCommand cmd = new SqlCommand("Update tbl_Tution_Fee_Details set MobileNo='" + txtmobile.Text + "', Fee='" + txtfee.Text + "'where ID='"+userid+"'", con);
cmd.ExecuteNonQuery();
con.Close();
Display();
}
protected void gvd_Fee_Display_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvd_Fee_Display.EditIndex = -1;
Display();
}
}
}