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
//To Add Esit,Update,Cancel in gridview control .....
//source
<asp:CommandField ShowEditButton="True" HeaderText="Edit Product Name & 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();
}
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)
.