GridView OnRowUpdating事件

问题描述:


我正在尝试通过RowUpdating事件更新gridview.我正在使用以下代码来更新gridview,但是它是新更新的.请帮助我确定代码中的问题.

Hi,
I am trying to update a gridview through RowUpdating event. I am using the following code to update gridview but it is new being updated. Please help me identify the issue with the code.

protected void UpdateRecord(object sender, GridViewUpdateEventArgs e)
   {
     GridViewRow row = grdCountry.Rows[e.RowIndex];

     ds = ((DataSet)(Session["dsKey"]));
     grdCountry.DataSource = ds;
     grdCountry.DataBind();
   }

代码中没有任何行显示您正在尝试更新记录.
No line in the code shows that you are trying to update a record.


在您的代码,您尚未从UI更新数据集,因此如何获取ui上的更新值.
做这样的事情
In your code, you have not updated your dataset from the UI so how can you can you get updated value on ui.
Do something like this
protected void UpdateRecord(object sender, GridViewUpdateEventArgs e)
 {
   //Retrieve the table from the session object.
   DataTable dt = (DataTable)Session["dsKey"];
   //Update the values.
   GridViewRow row = grdCountrygrdCountry.Rows[e.RowIndex];
   dt.Rows[row.DataItemIndex]["Id"] = ((TextBox)(row.Cells[1].Controls[0])).Text;
   dt.Rows[row.DataItemIndex]["Description"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
   dt.Rows[row.DataItemIndex]["IsComplete"] = ((CheckBox)(row.Cells[3].Controls[0])).Checked;
   //Reset the edit index.
   grdCountry.EditIndex = -1;
   //Bind data to the GridView control.
   grdCountry.DataSource = dt;
   grdCountry.DataBind();
 }


受保护的无效GridView1_RowUpdating(对象发送者,GridViewUpdateEventArgs e)
{
试试
{
int id = Convert.ToInt32(((Label)(GridView1.Rows [e.RowIndex] .FindControl("lbl_AssetID"))).Text);
//TextBox txtId =(TextBox)(GridView1.Rows [e.RowIndex] .FindControl("txtAssteId"));
//TextBox txtItemid =(TextBox)(GridView1.Rows [e.RowIndex] .FindControl("txtItemId")));
TextBox txtName =(TextBox)(GridView1.Rows [e.RowIndex] .FindControl("txtAssetName"));
TextBox txtAssetData =(TextBox)(GridView1.Rows [e.RowIndex] .FindControl("txtAssetData"));
DropDownList ddlType =(DropDownList)(GridView1.Rows [e.RowIndex] .FindControl("ddlAsetType"));
DropDownList ddlSubType =(DropDownList)(GridView1.Rows [e.RowIndex] .FindControl("ddlSubType"));
TextBox txtAssetFilepath =(TextBox)(GridView1.Rows [e.RowIndex] .FindControl("txtAssetFilePath"));
//Session ["txtItem"] = txtItemid.Text;
Session ["AssetId"] = id;
字符串sqlUpdate =更新资产集AssetType =""+ ddlType.SelectedItem.Text +",AssetSubType =""+ ddlSubType.SelectedItem.Text +",AssetName =""+ txtName.Text +" '',AssetData =''+ txtAssetData.Text +"'',AssetFilePath =''" + txtAssetFilepath.Text +''其中AssetId =''" + id +''";
如果(myConnection.State == ConnectionState.Closed)
{
myConnection.Open();
}
//OdbcCommand myCommand =新的OdbcCommand(sqlUpdate,myConnection);
命令=新的MySqlCommand(sqlUpdate,myConnection);
command.CommandType = CommandType.Text;
command.CommandText = sqlUpdate;
command.ExecuteNonQuery();
myConnection.Close();
lblMsg.Visible = true;
lblMsg.Text =资产更新成功.";
BindGrid();
Response.Redirect("AssetViewer.aspx?Sucess =" +"message");
}
catch(ex ex例外)
{
lblMsg.Visible = true;
lblMsg.Text = ex.Message;
}
}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
int id = Convert.ToInt32(((Label)(GridView1.Rows[e.RowIndex].FindControl("lbl_AssetID"))).Text);
//TextBox txtId = (TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtAssteId"));
//TextBox txtItemid = (TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtItemId"));
TextBox txtName = (TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtAssetName"));
TextBox txtAssetData = (TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtAssetData"));
DropDownList ddlType = (DropDownList)(GridView1.Rows[e.RowIndex].FindControl("ddlAsetType"));
DropDownList ddlSubType = (DropDownList)(GridView1.Rows[e.RowIndex].FindControl("ddlSubType"));
TextBox txtAssetFilepath = (TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtAssetFilePath"));
//Session["txtItem"] = txtItemid.Text;
Session["AssetId"] = id;
string sqlUpdate = "Update assets set AssetType=''" + ddlType.SelectedItem.Text + "'',AssetSubType=''" + ddlSubType.SelectedItem.Text + "'',AssetName=''" + txtName.Text + "'',AssetData=''" + txtAssetData.Text + "'',AssetFilePath=''" + txtAssetFilepath.Text + "'' where AssetId=''" + id + "''";
if (myConnection.State == ConnectionState.Closed)
{
myConnection.Open();
}
//OdbcCommand myCommand = new OdbcCommand(sqlUpdate, myConnection);
command = new MySqlCommand(sqlUpdate, myConnection);
command.CommandType = CommandType.Text;
command.CommandText = sqlUpdate;
command.ExecuteNonQuery();
myConnection.Close();
lblMsg.Visible = true;
lblMsg.Text = "Asset Updated Successfully.";
BindGrid();
Response.Redirect("AssetViewer.aspx?Sucess=" + "message");
}
catch (Exception ex)
{
lblMsg.Visible = true;
lblMsg.Text = ex.Message;
}
}

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindGrid();
    }