ASP.NET - working with GridView Programmatically

前端 未结 2 1722
孤街浪徒
孤街浪徒 2021-01-16 10:08

I am continuing from this post.

After much Googling, I have come up with this code to edit cells programmatically:

using System;
using System.Data;
u         


        
相关标签:
2条回答
  • 2021-01-16 10:37
    //To Add Esit,Update,Cancel in gridview control ..... 
    //source 
    <asp:CommandField ShowEditButton="True" HeaderText="Edit Product Name &amp; Price" />
    
    //c# code
      public void gris()
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = new SqlCommand("select * from Product_details", con);
                DataSet ds = new DataSet();
                da.Fill(ds);
                Gridview1.DataSource = ds;
                Gridview1.DataBind();
                con.Close();
            }
     protected void Gridview1_RowEditing(object sender, GridViewEditEventArgs e)
            {
                Gridview1.EditIndex = e.NewEditIndex;
                gris();
            }
    
            protected void Gridview1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
            {
                Gridview1.EditIndex = -1;
                gris();
            }
    
            protected void Gridview1_RowUpdating(object sender, GridViewUpdateEventArgs e)
            {
                string id = addproduct2.DataKeys[e.RowIndex].Value.ToString();
                string name = ((TextBox)addproduct2.Rows[e.RowIndex].Cells[1].Controls[0]).Text;//(or)Controls[1]
                string rate = ((TextBox)addproduct2.Rows[e.RowIndex].Cells[4].Controls[0]).Text;//(or)Controls[1]
                con.Open();
                cmd = new SqlCommand("update Product_details set product_name='" + name + "',Product_rate='" + rate + "',product_unitprice='" + rate + "' where product_id='" + id + "'", con);
                int i = cmd.ExecuteNonQuery();
                if (i >= 1)
                {
                    Response.Write("Updated");
                }
                con.Close();
                Gridview1.EditIndex = -1;
                gris();
    
            }
    
    0 讨论(0)
  • 2021-01-16 10:43

    for problem #1,try

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {
            GridView1.DataSource = Course.GetCourses();
            GridView1.DataBind();
            }
        }
    

    for problem #2, we need to check your method Course.Update(item) .

    0 讨论(0)
提交回复
热议问题