Thursday, February 28, 2013

data grig clear in c#






gridView1.DataSource = null;
gridView1.DataBind();





===============
this.dataGridView1.Rows.Clear();

=============
 dataGridView1.Rows.Clear();

-------------------
((DataTable)dataGridView1.DataSource).Rows.Clear();
***************************
------------
dataGridView1.Rows.Clear();
dataGridView1.Refresh();
-------------

stock Entry


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;


namespace Rice
{
    public partial class StockEntry : Form
    {
        OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=E:\\Rice\\Rice.mdb");

        public StockEntry()
        {
            InitializeComponent();
        }
        private void Idgen()
        {
            int id = 0;
            String str1 = "Select MAX(Riceid) from StockEntry";
            OleDbCommand cmd = new OleDbCommand(str1, con);
            con.Open();
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                if (dr.IsDBNull(0) == false)
                {
                    id = dr.GetInt32(0) + 1;
                }
                else
                {
                    id = 1;
                }
            }
            cbxRiceId.Text = id.ToString();
            dr.Close();
            con.Close();
        }
        private void Idshow()
        {
            cbxRiceId.Items.Clear();
            cbxRiceId.Text = "Select";
            String str = "Select * from StockEntry";
            OleDbCommand cmd = new OleDbCommand(str, con);
            con.Open();
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                if (dr.IsDBNull(0) == false)
                {
                    cbxRiceId.Items.Add(dr.GetValue(0).ToString());
                }
            }
            dr.Close();
            con.Close();
        }


        private void TextClear()
        {
            txtRname.Text  = "";
         cbxRunit .Text  = "";
         txtRUPkg.Text  = "";
            cbxRiceId.Text = "Select";
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TextClear();
            Idgen();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (cbxRiceId.Text == "Select")
            {
                MessageBox.Show("Select The Rice Id");
                cbxRiceId.Focus();
                return;
            }
            else
            {
                String str = "Select * from StockEntry where Riceid=" + cbxRiceId.Text + "";
                OleDbCommand cmd = new OleDbCommand(str, con);
                con.Open();
                OleDbDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    if (dr.IsDBNull(0) == false)
                    {
                        txtRname.Text = dr.GetValue(1).ToString();
                       cbxRunit .Text  = dr.GetValue(2).ToString();
                       txtRUPkg.Text  = dr.GetValue(3).ToString();
                    }
                }
                dr.Close();
                con.Close();
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String str1 = "Select * from StockEntry";
            OleDbCommand cmd1 = new OleDbCommand(str1, con);
            con.Open();
            OleDbDataReader dr1 = cmd1.ExecuteReader();
            while (dr1.Read())
            {
                if (dr1.IsDBNull(0) == false)
                {
                    string rid = dr1.GetValue(0).ToString();
                    if (cbxRiceId.Text == rid)
                    {
                        MessageBox.Show("This Number Already Exist");
                        cbxRiceId.Focus();
                        con.Close();
                        goto l1;
                    }
                }
            }
            con.Close();

            if (cbxRiceId.Text == "Select")
            {
                MessageBox.Show("Select The Rice Id");
                cbxRiceId.Focus();
                return;
            }
            if (txtRname.Text  == "")
            {
                MessageBox.Show("Enter The Brand Name");
                txtRname.Focus();
                return;
            }
            if (cbxRunit .Text  == "")
            {
                MessageBox.Show("Enter The No Unit     Rice");
                cbxRunit.Focus();
                return;
            }
            if (txtRUPkg.Text == "")
            {
                MessageBox.Show("Enter The Rice Unit Per Kg");
                txtRUPkg.Focus();
                return;
            }

            String str = "Insert into StockEntry(Riceid,Ricename,Riceunit,Ricepkg)Values(" + cbxRiceId.Text + ",'" + txtRname.Text  + "'," + cbxRunit.Text  + "," + txtRUPkg .Text  + ")";
            OleDbCommand cmd = new OleDbCommand(str, con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("Saved");
            Idshow();
            TextClear();



        l1:
            dr1.Close();
        }

        private void updateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (cbxRiceId.Text == "Select")
            {
                MessageBox.Show("Select The Rice Id");
                cbxRiceId.Focus();
                return;
            }
            else
            {
                String str = "Update StockEntry SET Ricename='" + txtRname.Text + "',Riceunit=" + cbxRunit.Text + ",Ricepkg=" + txtRUPkg.Text + " where Riceid=" + cbxRiceId.Text + "";
                OleDbCommand cmd = new OleDbCommand(str, con);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                MessageBox.Show("Updated");
                Idshow();
                TextClear();

            }
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (cbxRiceId.Text == "Select")
            {
                MessageBox.Show("Select The Rice Id");
                cbxRiceId.Focus();
                return;
            }
            else
            {
                String str = "Delete * from StockEntry where Riceid=" + cbxRiceId.Text + "";
                OleDbCommand cmd = new OleDbCommand(str, con);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                MessageBox.Show("Deleted");
                Idshow();
                TextClear();
            }
        }

        private void StockEntry_Load(object sender, EventArgs e)
        {
            cbxRiceId.Text = "Select";
            String str = "Select * from StockEntry";
            OleDbCommand cmd = new OleDbCommand(str, con);
            con.Open();
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                if (dr.IsDBNull(0) == false)
                {
                    cbxRiceId.Items.Add(dr.GetValue(0).ToString());
                }
            }
            dr.Close();
            con.Close();
        }
        private void OnlyNumberWithDot(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsNumber(e.KeyChar)) && (e.KeyChar != 8) && (e.KeyChar != 46))
            {
                e.Handled = true;
            }
        }
        private void OnlyNumber(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && (e.KeyChar != 8))
            {
                e.Handled = true;
            }
        }
        private void cbxRunit_KeyPress(object sender, KeyPressEventArgs e)
        {
            OnlyNumberWithDot(sender, e);
        }
        private void txtRUPkg_KeyPress(object sender, KeyPressEventArgs e)
        {
            OnlyNumberWithDot(sender, e);
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.ExitThread();
        }

        private void txtRUPkg_TextChanged(object sender, EventArgs e)
        {

        }

        private void cbxRunit_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbxRunit.Text != "")
            {

                txtRUPkg.Text = ((Convert.ToInt32(cbxRunit.Text) * 25).ToString());
            }
           
        }

    }
}

supplier information


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;


namespace Rice
{
    public partial class SupplierInfo : Form
    {
        OleDbConnection con = new OleDbConnection("Provider=microsoft.jet.oledb.4.0;data source=E:\\Rice\\Rice.mdb");
        public SupplierInfo()
        {
            InitializeComponent();
        }
        private void TextClear()
        {
            cbxSupplierid.Text = "Select";
            txtAddress.Text = "";
            txtBalamount.Text = "";
            txtContactno.Text = "";
            txtEmail.Text = "";
            txtMobile.Text = "";
            txtSname.Text = "";
        }
        private void Idgen()
        {
            int id = 0;
            String str2 = "select MAX(Supid) from Supinfo";
            OleDbCommand cmd2 = new OleDbCommand(str2, con);
            con.Open();
            OleDbDataReader dr2 = cmd2.ExecuteReader();
            while (dr2.Read())
            {
                if (dr2.IsDBNull(0) == false)
                {
                    id = dr2.GetInt32(0) + 1;
                }
                else
                    id = 1;
            }
            cbxSupplierid.Text = id.ToString();
            cbxSupplierid.Focus();
            dr2.Close();
            con.Close();
        }
        private void Idshow()
        {

            cbxSupplierid.Items.Clear();
            String st1 = "Select * from Supinfo order by Supid";
            OleDbCommand cm = new OleDbCommand(st1, con);
            con.Open();
            OleDbDataReader dr = cm.ExecuteReader();
            while (dr.Read())
            {
                if (dr.IsDBNull(0) == false)
                {
                    cbxSupplierid.Items.Add(dr.GetValue(0).ToString());
                }
            }
            dr.Close();
            con.Close();
        }


        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtBalamount.Enabled = false;
            TextClear();
            cbxSupplierid.Focus();
            Idgen();
        }
        private void OnlyNumberWithDot(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsNumber(e.KeyChar)) && (e.KeyChar != 8) && (e.KeyChar != 46))
            {
                e.Handled = true;
            }
        }
        private void OnlyNumber(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && (e.KeyChar != 8))
            {
                e.Handled = true;
            }
        }
        private void txtMobile_KeyPress(object sender, KeyPressEventArgs e)
        {
            OnlyNumber(sender, e);

        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (cbxSupplierid.Text == "Select")
            {
                MessageBox.Show("Select The Supplier ID");
                cbxSupplierid.Focus();
                return;
            }
            else
            {
                String str = "Select * from Supinfo where Supid=" + cbxSupplierid.Text + "";
                OleDbCommand cmd = new OleDbCommand(str, con);
                con.Open();
                OleDbDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    if (dr.IsDBNull(0) == false)
                    {
                        txtSname.Text = dr.GetValue(1).ToString();
                        txtAddress.Text = dr.GetValue(2).ToString();
                        txtContactno.Text = dr.GetValue(3).ToString();
                        txtMobile.Text = dr.GetValue(4).ToString();
                        txtEmail.Text = dr.GetValue(5).ToString();
                        txtBalamount.Text = dr.GetValue(6).ToString();
                    }
                }
                dr.Close();
                con.Close();
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtBalamount.Enabled = false;
            String str1 = "Select * from Supinfo";
            OleDbCommand cmd1 = new OleDbCommand(str1, con);
            con.Open();
            OleDbDataReader dr1 = cmd1.ExecuteReader();
            while (dr1.Read())
            {
                if (dr1.IsDBNull(0) == false)
                {
                    string sid = dr1.GetValue(0).ToString();
                    if (cbxSupplierid.Text == sid)
                    {
                        MessageBox.Show("This Number Already Exist");
                        cbxSupplierid.Focus();
                        con.Close();
                        goto l2;
                    }
                }
            }
            con.Close();

            if (cbxSupplierid.Text == "Select")
            {
                MessageBox.Show("Select The Supplier ID");
                cbxSupplierid.Focus();
                return;
            }
            if (txtSname.Text == "")
            {
                MessageBox.Show("Enter The Supplier Name");
                txtSname.Focus();
                return;
            }
            if (txtAddress.Text == "")
            {
                MessageBox.Show("Enter The Address");
                txtAddress.Focus();
                return;
            }
            if (txtContactno.Text == "")
            {
                MessageBox.Show("Enter The Contact Number");
                txtContactno.Focus();
                return;
            }
            if (txtMobile.Text == "")
            {
                MessageBox.Show("Enter The Mobile Number");
                txtMobile.Focus();
                return;
            }
            if (txtEmail.Text == "")
            {
                MessageBox.Show("Enter The Email Id");
                txtEmail.Focus();
                return;
            }

            //if (txtBalamount.Text == "")
            //{
            //    MessageBox.Show("Enter The Balance Amount");
            //    txtBalamount.Focus();
            //    return;
            //}
         

            String str = "Insert into Supinfo(Supid,Supname,Address,Contactno,Mobile,Email)Values(" + cbxSupplierid.Text + ",'" + txtSname.Text + "','" + txtAddress.Text + "','" + txtContactno.Text + "','" + txtMobile.Text + "','" + txtEmail.Text + "')";
            OleDbCommand cmd = new OleDbCommand(str, con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("Details Saved");
            TextClear();
            Idshow();


        l2:
            dr1.Close();
        }

        private void updateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (cbxSupplierid.Text == "Select")
            {
                MessageBox.Show("Select The Supplier ID");
                cbxSupplierid.Focus();
                return;
            }
            else
            {
                String str = "Update Supinfo SET Supname='" + txtSname.Text + "',Address='" + txtAddress.Text + "',Contactno='" + txtContactno.Text + "',Mobile='" + txtMobile.Text + "',Email='" + txtEmail.Text + "'  where Supid=" + cbxSupplierid.Text + "";
                OleDbCommand cmd = new OleDbCommand(str, con);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                MessageBox.Show("Details Updated");
                TextClear();
                Idshow();
            }
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (cbxSupplierid.Text == "Select")
            {
                MessageBox.Show("Select The Supplier ID");
                cbxSupplierid.Focus();
                return;
            }
            else
            {
                String str = "Delete * from Supinfo where Supid=" + cbxSupplierid.Text + "";
                OleDbCommand cmd = new OleDbCommand(str, con);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                MessageBox.Show("Details Deleted");
                TextClear();
                Idshow();
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SupplierInfo_Load(object sender, EventArgs e)
        {
         
           cbxSupplierid.Text = "Select";
            String str = "Select * from Supinfo";
            OleDbCommand cmd = new OleDbCommand(str, con);
            con.Open();
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                if (dr.IsDBNull(0) == false)
                {
                    cbxSupplierid.Items.Add(dr.GetValue(0).ToString());
                }
            }
            dr.Close();
            con.Close();

            cbxSupplierid.Focus();

        }

        private void cbxSupplierid_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}

Wednesday, February 27, 2013

c# update codes


The UpdateProduct Method (C#)
public class ProductDAL
{
    ...
   
    public static void UpdateProduct(int original_ProductID,
      string productName, decimal unitPrice, int unitsInStock)
    {
        // Updates the Products table
        string sql = "UPDATE Products SET ProductName = " +
           "@ProductName, UnitPrice = @UnitPrice, UnitsInStock = " +
           "@UnitsInStock WHERE ProductID = @ProductID";
        using (SqlConnection myConnection =
            new
SqlConnection(ConfigurationManager.ConnectionStrings["NWConnectionString"].ConnectionString))
        {
            SqlCommand myCommand = new SqlCommand(sql, myConnection);
            myCommand.Parameters.Add(new SqlParameter("@ProductName",
              productName));
            myCommand.Parameters.Add(new SqlParameter("@UnitPrice",
              unitPrice));
            myCommand.Parameters.Add(new SqlParameter("@UnitsInStock",
               unitsInStock));
            myCommand.Parameters.Add(new SqlParameter("@ProductID",
               original_ProductID));
            myConnection.Open();
            myCommand.ExecuteNonQuery();
            myConnection.Close();
        }
    }
}
----------------------------------------------------------------------------------------------------

 public void DAL_UpdateStudentsTable(DataTable table)
        {
            using (SqlConnection sqlConn = new SqlConnection(connString))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = @"UPDATE Students SET " +
                                       "StudentID = @id, " +
                                       "FirstName = @first, " +
                                       "LastName = @last, " +
                                       "Birthday = @birthday, " +
                                       "PersonalNo = @personal " +
                                       "WHERE StudentID = @oldId";
                    cmd.Parameters.Add("@id", SqlDbType.Int, 5, "StudentID");
                    cmd.Parameters.Add("@first", SqlDbType.VarChar, 50, "FirstName");
                    cmd.Parameters.Add("@last", SqlDbType.VarChar, 50, "LastName");
                    cmd.Parameters.Add("@birthday", SqlDbType.DateTime, 1, "Birthday");
                    cmd.Parameters.Add("@personal", SqlDbType.VarChar, 50, "PersonalNo");
                    SqlParameter param = cmd.Parameters.Add("@oldId", SqlDbType.Int, 5, "StudentID");
                    param.SourceVersion = DataRowVersion.Original;
                    cmd.Connection = sqlConn;
                    using (SqlDataAdapter da = new SqlDataAdapter())
                    {
                        da.UpdateCommand = cmd;
                        da.Update(table);
                    }
                }
            }
        }
----------------------------------------------------------------------------------------------
A sample code

using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using KMS.DAO;
using KMS.Database_Connection;
using System.Data.Odbc;
using KMS.ENTITY;
using KMS.CommanUtility;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace KMS.DAO
{
public class AHBDAO
{
Services servi = new Services();

/*Insert a row into database*/
public void Insert(AHBEntity obj)
{
int rwaffect;

try
{
String insertStr = "INSERT INTO tbluser (" +
"fullname,pkuserid,name,ITZone,ministry,offices,Address,Email" +
") VALUES ('" + obj.Name + "'," + obj.Pkuserid + ",'" + obj.Name + "','" + obj.Zone + "'," + obj.Ministry + ",'" + obj.Offices + "','" + obj.Address1 + "','" + obj.Email1 + "')";

rwaffect = servi.ExecuteQuery(insertStr);
servi.CloseResource();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}

/*Delete a row from database*/
public void delete(AHBEntity del)
{
try
{
int rwaffect;
String deleteStr = "DELETE FROM tbluser WHERE pkid="+del.Pkuserid+" ";
rwaffect = servi.ExecuteQuery(deleteStr);
servi.CloseResource();
}
catch
{

}
}

/*Set current properties from database*/
//public AHBEntity select(AHBEntity s)
//{
// // AHBEntity AHBobj = null;
// //// String selectStr = "SELECT u.pkuserid,u.name, u.ITZone, u.ministry, " +
// // "u.Offices, u.Address, u.Email,d.designation,d.phone,d.phone2 FROM tbluser u,mstbldesignation d" +
// // " WHERE pkuserid=" + s.Pkuserid + " and u.pkuserid="+s.Pkid+"";
// // OdbcDataReader result;
// // result = servi.ExecuteReader(selectStr);
// // if (result.HasRows)
// // {
// // AHBobj = new AHBEntity();
// // //iteminpanelobj = new TbliteminpanelEntity();

// // AHBobj.Pkuserid = (result.IsDBNull(0) ? "" : Convert.ToString(result.GetDecimal(0)));
// // AHBobj.Name = (result.IsDBNull(1) ? "" : result.GetString(1));
// // AHBobj.Zone = (result.IsDBNull(2) ? "" :Convert.ToString(result.GetInt32(2)));
// // AHBobj.Ministry = (result.IsDBNull(3) ? "" : Convert.ToString(result.GetInt32(3)));
// // AHBobj.Offices = (result.IsDBNull(4) ? "" : result.GetString(4));
// // AHBobj.Address1 = (result.IsDBNull(5) ? "" : result.GetString(5));
// // AHBobj.Email1 = (result.IsDBNull(6) ? "" : result.GetString(6));
// // AHBobj.Designation = (result.IsDBNull(7) ? "" : result.GetString(7));
// // AHBobj.Phone = (result.IsDBNull(8) ? "" : result.GetString(8));
// // AHBobj.Phone2 = (result.IsDBNull(9) ? "" : result.GetString(9));

// // }
// // servi.CloseResource();
// // return AHBobj;

//}

/*Update database*/
public int update(AHBEntity obj)
{

int rawAffected;

String updateStr = "UPDATE tbluser SET " +
"pkuserid=?,name=?,ITzone=?,ministry=?,offices=?,Address=?,Email=? WHERE pkuserid=? ";
rawAffected = servi.ExecuteQuery(updateStr);
servi.CloseResource();
return rawAffected;
}

/*Auto-Increment Id*/
public int selectMaxPkId()
{
int maxpkid = 0;
String selectStmt = "select max(pkuserid) from tbluser";
OdbcDataReader result;
result = servi.ExecuteReader(selectStmt);
if (result.HasRows)
{
maxpkid = result.GetInt32(0);
maxpkid++;
}
return maxpkid;
}

/*List value*/
public ArrayList listAHB()
{

OdbcDataReader result;
ArrayList listAHB = new ArrayList();
AHBEntity AHBobj;

String selectSt = "SELECT u.pkuserid,u.name, u.ITZone, u.ministry, u.Offices, u.Address, u.Email,d.designation,d.phone,d.phone2 FROM tbluser u,mstbldesignation d WHERE u.pkuserid = d.pkid";

result = servi.ExecuteReader(selectSt);
if (result.HasRows)
{
while (result.Read())
{
AHBobj = new AHBEntity();
AHBobj.Pkuserid = (result.IsDBNull(0) ? "" : Convert.ToString(result.GetDecimal(0)));
AHBobj.Name = (result.IsDBNull(1) ? "" : result.GetString(1));
AHBobj.Zone = (result.IsDBNull(2) ? "" : Convert.ToString(result.GetInt32(2)));
AHBobj.Ministry = (result.IsDBNull(3) ? "" : Convert.ToString(result.GetInt32(3)));
AHBobj.Offices = (result.IsDBNull(4) ? "" : result.GetString(4));
AHBobj.Address1 = (result.IsDBNull(5) ? "" : result.GetString(5));
AHBobj.Email1 = (result.IsDBNull(6) ? "" : result.GetString(6));
AHBobj.Designation = (result.IsDBNull(7) ? "" : result.GetString(7));
AHBobj.Phone = (result.IsDBNull(8) ? "" : result.GetString(8));
AHBobj.Phone2 = (result.IsDBNull(9) ? "" : result.GetString(9));

listAHB.Add(AHBobj);
}
}
servi.CloseResource();
return listAHB;
}
}
}

--------------------------------------------------
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillGridView();
        }
    }
    /// <summary>
    /// Fill record in gridview
    /// </summary>
    public void FillGridView()
    {
        try
        {
            string cnString = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
            SqlConnection con = new SqlConnection(cnString);
            GlobalClass.adap = new SqlDataAdapter("select * from gridvew", con);
            SqlCommandBuilder bui = new SqlCommandBuilder(GlobalClass.adap);
            GlobalClass.dt = new DataTable();
            GlobalClass.adap.Fill(GlobalClass.dt);
            GridView1.DataSource = GlobalClass.dt;
            GridView1.DataBind();
        }
        catch
        {
         
        }
    }
    protected void editRecord(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        FillGridView();
    }
    protected void cancelRecord(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        FillGridView();
    }

    /// <summary>
    /// New Row Add
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void AddNewRecord(object sender, EventArgs e)
    {
        try
        {
            if (GlobalClass.dt.Rows.Count > 0)
            {
                GridView1.EditIndex = -1;
                GridView1.ShowFooter = true;
                FillGridView();
            }
            else
            {      
                GridView1.ShowFooter = true;
                DataRow dr = GlobalClass.dt.NewRow();
                dr[1] = "0";
                dr[2] = 0;
                dr[3] = 0;
                dr[4] = "0";
                dr[5] = "0";
                GlobalClass.dt.Rows.Add(dr);
                GridView1.DataSource = GlobalClass.dt;
                GridView1.DataBind();
                GridView1.Rows[0].Visible = false;
            }
        }
        catch
        {

        }
    }

    /// <summary>
    /// New Record Cancel
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void AddNewCancel(object sender, EventArgs e)
    {
        GridView1.ShowFooter = false;
        FillGridView();
    }

    /// <summary>
    /// Insert New Record
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void InsertNewRecord(object sender, EventArgs e)
    {
        try
        {
            string strName = GlobalClass.dt.Rows[0]["name"].ToString();
            if (strName == "0")
            {
                GlobalClass.dt.Rows[0].Delete();
                GlobalClass.adap.Update(GlobalClass.dt);
            }
         
                TextBox txtName = GridView1.FooterRow.FindControl("txtNewName") as TextBox;
                TextBox txtAge = GridView1.FooterRow.FindControl("txtNewAge") as TextBox;
                TextBox txtSalary = GridView1.FooterRow.FindControl("txtNewSalary") as TextBox;
                TextBox txtCountry = GridView1.FooterRow.FindControl("txtNewCountry") as TextBox;
                TextBox txtCity = GridView1.FooterRow.FindControl("txtNewCity") as TextBox;

                DataRow dr = GlobalClass.dt.NewRow();
                dr["name"] = txtName.Text.Trim();
                dr["age"] = txtAge.Text.Trim();
                dr["salary"] = txtSalary.Text.Trim();
                dr["country"] = txtCountry.Text.Trim();
                dr["city"] = txtCity.Text.Trim();
                GlobalClass.dt.Rows.Add(dr);
                GlobalClass.adap.Update(GlobalClass.dt);
                GridView1.ShowFooter = false;
                FillGridView();
         
        }
        catch
        {

        }

    }
    /// <summary>
    /// Record Updation
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void updateRecord(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            TextBox txtName = GridView1.Rows[e.RowIndex].FindControl("txtName") as TextBox;
            TextBox txtAge = GridView1.Rows[e.RowIndex].FindControl("txtAge") as TextBox;
            TextBox txtSalary = GridView1.Rows[e.RowIndex].FindControl("txtSalary") as TextBox;
            TextBox txtCountry = GridView1.Rows[e.RowIndex].FindControl("txtCountry") as TextBox;
            TextBox txtCity = GridView1.Rows[e.RowIndex].FindControl("txtCity") as TextBox;

            GlobalClass.dt.Rows[GridView1.Rows[e.RowIndex].RowIndex]["name"] = txtName.Text.Trim();
            GlobalClass.dt.Rows[GridView1.Rows[e.RowIndex].RowIndex]["age"] = Convert.ToInt32(txtAge.Text.Trim());
            GlobalClass.dt.Rows[GridView1.Rows[e.RowIndex].RowIndex]["salary"] = Convert.ToInt32(txtSalary.Text.Trim());
            GlobalClass.dt.Rows[GridView1.Rows[e.RowIndex].RowIndex]["country"] = txtCountry.Text.Trim();
            GlobalClass.dt.Rows[GridView1.Rows[e.RowIndex].RowIndex]["city"] = txtCity.Text.Trim();
            GlobalClass.adap.Update(GlobalClass.dt);
            GridView1.EditIndex = -1;
            FillGridView();
        }
        catch
        {
         
        }
    }

    /// <summary>
    /// Record Deletion
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            GlobalClass.dt.Rows[GridView1.Rows[e.RowIndex].RowIndex].Delete();
            GlobalClass.adap.Update(GlobalClass.dt);
            FillGridView();
        }
        catch
        {

        }
    }
}

-----------------------------------------------------------------------------------------