2014年7月13日 星期日

[RESOLVED] Save GV records


Hi,

I have a Gridview to show the detail records on the page. Upon saving the header record on the page, do I need to specifically fire

OnRowUpdating



event of the Gridview to save the detail records?





I have the RowUpdate event like


protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex] as GridViewRow;
int obj_id = Convert.ToInt16(lb_userid.Text);
TextBox tb_ite_id = row.FindControl("tb_ite_id") as TextBox;
TextBox tb_ite_id2 = row.FindControl("tb_ite_id2") as TextBox;
TextBox tb_ite_desc = row.FindControl("tb_ite_desc") as TextBox;
Label lblchangedate = row.FindControl("lblchangedate") as Label;

using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Mssqlconn2"].ConnectionString))
{
string sql = "Update tab5 set ite_id = @ite_id,ite_desc=@ite_desc, change_date= @change_date" + " where obj_id=@obj_id and ite_id=@ite_id2";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddWithValue(
"@ite_id", Convert.ToInt16(tb_ite_id.Text));
cmd.Parameters.AddWithValue(
"@ite_desc", tb_ite_desc.Text.Trim());
cmd.Parameters.AddWithValue(
"@change_date", Convert.ToDateTime(lblchangedate.Text));
cmd.Parameters.AddWithValue(
"@obj_id", obj_id);
cmd.Parameters.AddWithValue(
"@ite_id2", Convert.ToInt16(tb_ite_id2.Text));
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
GridView1.EditIndex = -1;
this.PopulateData();
}







do I need to run this for each Gridview record? How about the way to call this event?



Any help to this?



protected void grd_RowUpdating(object sender, GridViewUpdateEventArgs e)

{

DataTable dt = (DataTable)ViewState["Item"];



int temprowindex = grd_order.PageIndex * grd_order.PageSize + e.RowIndex;



string itemname = dt.Rows[temprowindex]["item_Name"].ToString();

string id = grd.DataKeys[e.RowIndex].Value.ToString();



dt.Rows[temprowindex]["item_name"] = ((TextBox)grd_order.Rows[e.RowIndex].FindControl("txtitemnameedit")).Text.ToUpper();

dt.Rows[temprowindex]["sl_no"] = ((TextBox)grd_order.Rows[e.RowIndex].FindControl("txtsnoedit")).Text;


DropDownList ddlcategory = ((DropDownList)grd.Rows[e.RowIndex].FindControl("ddlItem_CategoryEdit"));

dt.Rows[temprowindex]["ItemCate_Code"] = ddlcategory.SelectedItem.Value;

string str = "UPDATE query";

AC.execute_nonquery(str);



ScriptManager.RegisterStartupScript(Page, typeof(Page), "abc12", "alert('Record Updated Sucessfully!!!')", true);


ViewState["Item"] = dt;


grd.EditIndex = -1;

grd.DataSource = dt;

grd.DataBind();





}



When is this RowUpdating event fired? How are the records in Gridview saved?





wmec



I have the RowUpdate event like
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex] as GridViewRow;
int obj_id = Convert.ToInt16(lb_userid.Text);
TextBox tb_ite_id = row.FindControl("tb_ite_id") as TextBox;
TextBox tb_ite_id2 = row.FindControl("tb_ite_id2") as TextBox;
TextBox tb_ite_desc = row.FindControl("tb_ite_desc") as TextBox;
Label lblchangedate = row.FindControl("lblchangedate") as Label;

using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Mssqlconn2"].ConnectionString))
{
string sql = "Update tab5 set ite_id = @ite_id,ite_desc=@ite_desc, change_date= @change_date" + " where obj_id=@obj_id and ite_id=@ite_id2";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddWithValue(
"@ite_id", Convert.ToInt16(tb_ite_id.Text));
cmd.Parameters.AddWithValue(
"@ite_desc", tb_ite_desc.Text.Trim());
cmd.Parameters.AddWithValue(
"@change_date", Convert.ToDateTime(lblchangedate.Text));
cmd.Parameters.AddWithValue(
"@obj_id", obj_id);
cmd.Parameters.AddWithValue(
"@ite_id2", Convert.ToInt16(tb_ite_id2.Text));
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
GridView1.EditIndex = -1;
this.PopulateData();
}







do I need to run this for each Gridview record? How about the way to call this event?





Hi,


You should first enable the edit button.


Then this RowUpdating event will fire once you have do some edit on the gridview row and click the update button in edit mode.


As you have used  GridViewRow row = GridView1.Rows[e.RowIndex] as GridViewRow, then it will find the current row, so you do not need to run this for each gridview record.


Best Regards.


沒有留言:

張貼留言