//更新数据
protected void grdAutomobile_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
#region 获取修改后的值
int id = Convert.ToInt32(grdAutomobile.DataKeys[e.RowIndex].Value.ToString());//主键ID
string Driver = ((TextBox)grdAutomobile.Rows[e.RowIndex].Cells[1].Controls[0]).Text.Trim();
string AutomobileLicensePlate = ((TextBox)grdAutomobile.Rows[e.RowIndex].Cells[2].Controls[0]).Text.Trim();
string AutomobileType = ((TextBox)grdAutomobile.Rows[e.RowIndex].Cells[3].Controls[0]).Text.Trim();
string AutomobileSeatings = ((TextBox)grdAutomobile.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
string AutomobileCapacity = ((TextBox)grdAutomobile.Rows[e.RowIndex].Cells[5].Controls[0]).Text;
bool Status = ((CheckBox)grdAutomobile.Rows[e.RowIndex].Cells[6].Controls[0]).Checked;
string Comments = ((TextBox)grdAutomobile.Rows[e.RowIndex].Cells[7].Controls[0]).Text.Trim();
#endregion
#region 验证非空
if (Driver == null || Driver == "")
{
UseUtility.Alert("汽车司机不能为空!", this.Page);
e.Cancel = true;
return;
}
if (AutomobileLicensePlate == null || AutomobileLicensePlate == "")
{
UseUtility.Alert("汽车牌照不能为空!", this.Page);
e.Cancel = true;
return;
}
if (AutomobileType == null || AutomobileType == "")
{
UseUtility.Alert("汽车型号不能为空!", this.Page);
e.Cancel = true;
return;
}
#endregion
#region 验证非法输入
int seat, Capacity;
try
{
seat = Convert.ToInt32(AutomobileSeatings); //座位号
Capacity = Convert.ToInt32(AutomobileCapacity); //载重
}
catch
{
UseUtility.Alert("输入有误,请重新输入!", this.Page);
e.Cancel = true;
return;
}
if (seat <= 0)
{
UseUtility.Alert("座位号必须大于零!", this.Page);
e.Cancel = true;
return;
}
if (Capacity <= 0)
{
UseUtility.Alert("载重必须大于零!", this.Page);
e.Cancel = true;
return;
}
#endregion
try
{
#region 赋值操作
XMSTC.Model.Automobile automobile = new XMSTC.Model.Automobile();
automobile = automobileBll.GetModel(id);
automobile.Driver = Driver;
automobile.AutomobileLicensePlate = AutomobileLicensePlate;
automobile.AutomobileType = AutomobileType;
automobile.AutomobileSeatings = seat;
automobile.AutomobileCapacity = Capacity;
automobile.AutomobileStatus = Status;
automobile.Comments = Comments;
#endregion
if (automobileBll.Update(automobile))
{
UseUtility.Alert("更新成功!", this.Page);
grdAutomobile.EditIndex = -1;
grdAutomobileBind();
}
else
{
UseUtility.Alert("更新失败!", this.Page);
grdAutomobile.EditIndex = -1;
grdAutomobileBind();
}
}
catch (Exception ex)
{
UseUtility.Alert(ex.ToString(), this.Page);
return;
}
}
gridview手写更新数据操作
最新推荐文章于 2023-08-11 21:29:02 发布