1. 程式人生 > >gridview單元格編輯新增資料

gridview單元格編輯新增資料

行號

private void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
            if (e.Info.IsRowIndicator)
            {
                if
(e.RowHandle >= 0) { e.Info.DisplayText = (e.RowHandle + 1).ToString(); } else if (e.RowHandle < 0 && e.RowHandle > -1000) { e.Info.Appearance.BackColor = System.Drawing.Color.AntiqueWhite; e.Info.DisplayText
= "G" + e.RowHandle.ToString(); } } }

宣告資料來源

private BindingList<InvoiceDetail> DataSource;//發票資料來源
//初始化資料來源
private void frmAddindent_Load(object sender, EventArgs e)
        {
            this.outTaskListLog.RefreshParent += new RefreshParentHandler((object
obj) => { SetForm((OutRequest)obj); }); DataSource = new BindingList<InvoiceDetail>(); dvginfo.DataSource = DataSource; }

 

新增新行事件

private void gridView1_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
        {
            InvoiceDetail invoiceData = gridView1.GetRow(e.RowHandle) as InvoiceDetail;
            invoiceData.Price = 0;
            invoiceData.Amount = 0;
            invoiceData.Currency = cbDeclaredCurrency.Text;
            invoiceData.Quantity = 1;
            invoiceData.QuantityUnit = "PCS";
            invoiceData.OriginCountryCode = "CN";
        }

單元格值,驗證,離開事件

 private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (gridView1.FocusedRowHandle >= 0 || gridView1.IsNewItemRow(gridView1.FocusedRowHandle))
            {
                if (e.Column.FieldName.Equals("Quantity") || e.Column.FieldName.Equals("Price"))
                {
                    int i = 0;
                    if (gridView1.GetFocusedRowCellValue("Quantity") != null)
                    {
                        i = (int)gridView1.GetFocusedRowCellValue("Quantity");
                    }
                    decimal d = 0;
                    if (gridView1.GetFocusedRowCellValue("Price") != null)
                    {
                        d = Common.Utils.ObjToDecimal(gridView1.GetFocusedRowCellValue("Price"), 0);
                    }
                    decimal dec = i * d;
                    //設定結果值
                    gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["Amount"], dec);
                    //gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["NumberOfPieces"], e.RowHandle + 1);
                }
                if (e.Column.FieldName.Equals("Amount"))
                {
                    decimal dAmount = DataSource.Sum(s => s.Amount);
                    txtDeclared.Text = dAmount.ToString();
                }
            }
        }
private void gridView1_BeforeLeaveRow(object sender, DevExpress.XtraGrid.Views.Base.RowAllowEventArgs e)
        {
            if (gridView1.GetFocusedRowCellValue("Description") == null || string.IsNullOrWhiteSpace(gridView1.GetFocusedRowCellValue("Description").ToString()))
            {
                //gridView1.SetFocusedValue(gridView1.GetFocusedRowCellValue(""));
                return;
            }
            if (gridView1.GetFocusedRowCellValue("Quantity") == null || gridView1.GetFocusedRowCellValue("Quantity").ToString() == "0")
            {
                //gridView1.OptionsView.NewItemRowPosition = NewItemRowPosition.None;
                return;
            }
            if (gridView1.GetFocusedRowCellValue("Price") == null || gridView1.GetFocusedRowCellValue("Price").ToString() == "0")
            {
                return;
            }
            if (gridView1.GetFocusedRowCellValue("Amount") == null || gridView1.GetFocusedRowCellValue("Amount").ToString() == "0")
            {
                return;
            }
        }

        private void gridView1_ValidatingEditor(object sender, DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs e)
        {
            if (e.Value == null)
            {
                e.Value = "";
            }
            string reply = "";
            int ireply = -1;
            decimal dreply = -1;
            if (gridView1.FocusedColumn.FieldName == "Description")
            {
                if (string.IsNullOrWhiteSpace(e.Value.ToString()))
                {
                    reply = "品名不能為空。";
                }
            }
            if (gridView1.FocusedColumn.FieldName == "Quantity")
            {
                if (int.TryParse(e.Value.ToString(), out ireply))
                {
                    if (ireply <= 0)
                    {
                        reply = "物品數量必須大於零";
                    }
                }
                else
                {
                    reply = "物品數量需要輸入整數";
                }

            }

            if (gridView1.FocusedColumn.FieldName == "Price")
            {
                if (decimal.TryParse(e.Value.ToString(), out dreply))
                {
                    if (dreply <= 0)
                    {
                        reply = "單件必須大於零";
                    }
                }
                else
                {
                    reply = "單件需要輸入數字";
                }
            }

            if (gridView1.FocusedColumn.FieldName == "Amount")
            {
                if (decimal.TryParse(e.Value.ToString(), out dreply))
                {
                    if (dreply <= 0)
                    {
                        reply = "金額必須大於零";
                    }

                }
                else
                {
                    reply = "金額需要輸入數字";
                }
            }

            if (!string.IsNullOrWhiteSpace(reply))
            {
                e.ErrorText = reply.ToString();
                e.Valid = false;
            }
        }

        AppearanceDefault appError = new AppearanceDefault(Color.White, Color.LightCoral, Color.Empty, Color.Red, System.Drawing.Drawing2D.LinearGradientMode.ForwardDiagonal);
        private void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            object val = gridView1.GetRowCellValue(e.RowHandle, e.Column);
            if (gridView1.IsNewItemRow(e.RowHandle))
            {
                return;
            }
            else
            {
                if (e.Column.FieldName == "Description")
                {
                    if (e.CellValue == null || string.IsNullOrWhiteSpace(e.CellValue.ToString()))
                    {
                        AppearanceHelper.Apply(e.Appearance, appError);
                        //ErrorInvoice = true;
                    }
                }
                if (e.Column.FieldName == "Quantity")
                {
                    if (e.CellValue == null || e.CellValue.ToString() == "0")
                    {
                        AppearanceHelper.Apply(e.Appearance, appError);
                        //ErrorInvoice = true;
                    }
                }
                if (e.Column.FieldName == "Price")
                {
                    if (e.CellValue == null || e.CellValue.ToString() == "0")
                    {
                        AppearanceHelper.Apply(e.Appearance, appError);
                    }
                }
                if (e.Column.FieldName == "Amount")
                {
                    if (e.CellValue == null || e.CellValue.ToString() == "0")
                    {
                        AppearanceHelper.Apply(e.Appearance, appError);
                    }
                }
            }
        }

單元格控制元件賦值

#region 發票明細資料
            //原產地
            gltxtCountryOrigin.DataSource = cname;//原產地二字碼
            this.gltxtCountryOrigin.NullText = "";
            this.gltxtCountryOrigin.DisplayMember = "Col001";
            this.gltxtCountryOrigin.ValueMember = "Col001";
            this.gltxtCountryOrigin.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
            this.gltxtCountryOrigin.View.BestFitColumns();
            this.gltxtCountryOrigin.ShowFooter = false;
            this.gltxtCountryOrigin.View.OptionsView.ShowAutoFilterRow = true; //顯示不顯示grid上第一個空行,也是用於檢索的應用
            //this.cmb_rCountryCode.Properties.AutoComplete = false;
            this.gltxtCountryOrigin.ImmediatePopup = true;////在輸入框按任一可見字元鍵時立即彈出下拉窗體
            this.gltxtCountryOrigin.PopupFilterMode = DevExpress.XtraEditors.PopupFilterMode.Contains;//
            this.gltxtCountryOrigin.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;
            //數量單位
            var mienu_QuantityUnits = quantityUnitsBLL.GetQuantityUnitsList().OrderBy(o => o.quCode).ToList();//數量單位
            this.gltxtNumberUnits.DataSource = mienu_QuantityUnits;
            this.gltxtNumberUnits.NullText = "";
            this.gltxtNumberUnits.DisplayMember = "quCode";
            this.gltxtNumberUnits.ValueMember = "quCode";
            this.gltxtNumberUnits.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
            this.gltxtNumberUnits.View.BestFitColumns();
            this.gltxtNumberUnits.ShowFooter = false;
            this.gltxtNumberUnits.View.OptionsView.ShowAutoFilterRow = true; //顯示不顯示grid上第一個空行,也是用於檢索的應用
            //this.gltxtNumberUnits.Properties.AutoComplete = false;
            this.gltxtNumberUnits.ImmediatePopup = true;////在輸入框按任一可見字元鍵時立即彈出下拉窗體
            this.gltxtNumberUnits.PopupFilterMode = DevExpress.XtraEditors.PopupFilterMode.Contains;//
            this.gltxtNumberUnits.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;
            #endregion