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;
        }
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章