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" %>







   


   

   

   
       
       
           
           
     
           


           
           
           
           
           
           
         
     
       

       
 
 
   

       <%----%>
   

   
 








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();
        }
    }
}