gridview手写更新数据操作

 
//更新数据
    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;
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值