Results 1 to 2 of 2

Thread: How to Edit, update delete in gridview

  1. #1
    WilsonMartin is offline Senior Member
    Join Date
    Dec 2009
    Posts
    319
    Rep Power
    3

    Default How to Edit, update delete in gridview

    Hi everyone. I am last year MCA student. I am education asp.net. For practice I am rising one website. In that I use gridview control. I want to Edit, update erase this gridview . I tried diverse none but I none of them work out. Please give me correct code which is helpful for me.

  2. #2
    AndersonDiaz is offline Senior Member
    Join Date
    Dec 2009
    Posts
    311
    Rep Power
    3

    Default

    Refer following program to edit, update delete in gridview in asp.net:

    Code:
    rotected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                bindgrid();
                total = 0;
            }
        }
        public void bindgrid()
        {
            SqlConnection conn = new SqlConnection("Data Source='localhost';Initial Catalog='Northwind';Integrated Security=SSPI;Persist Security Info=False ");
            SqlCommand cmd = new SqlCommand("select CategoryName,CategoryID from Categories ", conn);
            SqlDataAdapter da = new SqlDataAdapter("", conn);
            da.SelectCommand = new SqlCommand("select CategoryName,CategoryID from Categories", conn);
            DataSet ds = new DataSet();
            da.Fill(ds, "data");
            gdview.DataSource = ds.Tables[0].DefaultView;
            gdview.DataBind();
        }
        protected void gdview_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gdview.EditIndex = e.NewEditIndex;
            bindgrid();
        }
        protected void gdview_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int catid = int.Parse(gdview.DataKeys[e.RowIndex].Value.ToString());
            string strcatname=((TextBox)gdview.Rows[e.RowIndex].Cells[0].Controls[0]).Text;
            SqlConnection conn = new SqlConnection("Data Source='localhost';Initial Catalog='Northwind';Integrated Security=SSPI;Persist Security Info=False ");
            SqlDataAdapter da = new SqlDataAdapter("", conn);
            conn.Open();
            da.UpdateCommand = new SqlCommand("update Categories set CategoryName='" + strcatname + "' where CategoryID=" + catid, conn);
            da.UpdateCommand.ExecuteNonQuery();
            conn.Close();
            gdview.EditIndex = -1;
            bindgrid();
        }
        protected void gdview_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gdview.EditIndex = -1;
            bindgrid();
        }
        protected void gdview_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int catid = int.Parse(gdview.DataKeys[0].Value.ToString());
           SqlConnection conn = new SqlConnection("Data Source='localhost';Initial Catalog='Northwind';Integrated Security=SSPI;Persist Security Info=False ");
            SqlDataAdapter da = new SqlDataAdapter("", conn);
            conn.Open();
            da.DeleteCommand = new SqlCommand("delete from Categories where CategoryID="+catid, conn);
            da.DeleteCommand.ExecuteNonQuery();
            conn.Close();
            bindgrid();
        }

Similar Threads

  1. Bind a GridView Control to XML
    By BrooksGray in forum other peripherals
    Replies: 2
    Last Post: 07-28-2010, 02:36 PM
  2. How to bind gridview in asp.net
    By AdamsClark in forum other peripherals
    Replies: 2
    Last Post: 07-04-2010, 02:01 PM
  3. Bind gridview using code
    By ThomasBarnes in forum Programming
    Replies: 1
    Last Post: 04-29-2010, 01:37 PM
  4. Edit, update, and delete in giridview
    By CarterWatson in forum Programming
    Replies: 1
    Last Post: 04-03-2010, 12:20 PM
  5. How to Edit,Update,Delete in Gridview
    By GonzalezBrown in forum Operating System
    Replies: 1
    Last Post: 02-10-2010, 03:51 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
SEO by SubmitEdge

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48