Dev gridView中设置自适应列宽和日期显示格式、金额的显示格式

时间:2023-03-08 20:16:23
Dev gridView中设置自适应列宽和日期显示格式、金额的显示格式

在Dev GridView控件中,数据库中表数据日期都是长日期格式(yyyy-MM-dd HH:mm:ss),但显示在控件变成短日期格式(yyyy-MM-dd),金额显示要显示精确的数值,

比如80.00,90.15等,但在 dev gridView中只是显示80,90

1、解决日期显示问题的代码: 设置日期的displayformat,editFormat,EditMask

/// <summary>
        /// 设置时间显示格式
        /// </summary>
        /// <param name="dateEdit">日期时间的父类,就是日期控件</param>
        public static void SetDateTime(DateEdit dateEdit)
        {
            dateEdit.Properties.DisplayFormat.FormatString = "yyyy-MM-dd HH:mm";
            dateEdit.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            dateEdit.Properties.EditFormat.FormatString = "yyyy-MM-dd HH:mm";
            dateEdit.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            dateEdit.Properties.Mask.EditMask = "yyyy-MM-dd HH:mm";  
        
        }

2、解决金额显示精确问题

/// <summary>
        /// 设置价格的显示格式
        /// </summary>
        /// <param name="spinEdit"></param>
        public static void SetMoney(AokaSpinEdit spinEdit)
        {
            spinEdit.Properties.DisplayFormat.FormatString = "{0:N2}";
            spinEdit.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            spinEdit.Properties.EditFormat.FormatString = "{0:N2}";
            spinEdit.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
        }

上面是在明细中,调用控件的名称,设置在初始化类,比如 Hepper.SetDateTime(startDate);

3.解决列表显示时日期和金额的问题

/// <summary>
        /// 设置列表数据的日期格式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
           
            if (e.Column.FieldName == "创建日期" || e.Column.FieldName == "创建时间" || e.Column.FieldName == "StartDate" || e.Column.FieldName == "EndDate" || e.Column.FieldName == "发货时间" || e.Column.FieldName == "发生时间" || e.Column.FieldName == "制单日期" || e.Column.FieldName == "单据日期" || e.Column.FieldName == "BillDate")
            {
                e.Column.DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
            }         
           
        }
        /// <summary>
        /// 设置列表数据中价格的格式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void gridView1_RowCellStyle1(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            if (e.Column.FieldName == "价格元" || e.Column.FieldName == "Price" || e.Column.FieldName == "Money" || e.Column.FieldName == "应付款" || e.Column.FieldName == "应收款" || e.Column.FieldName == "单价" || e.Column.FieldName == "金额" || e.Column.FieldName == "总金额" || e.Column.FieldName == "合计金额")
            {
                e.Column.DisplayFormat.FormatString = "{0:N2}";
                e.Column.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            }
        }

在窗体下,是这样的调用的

    //列表日期显示格式
            this.gridView_List.RowCellStyle += new DevExpress.XtraGrid.Views.Grid.RowCellStyleEventHandler(Heppler.gridView1_RowCellStyle);
            //列表价格显示格式
            this.gridView_List.RowCellStyle += new DevExpress.XtraGrid.Views.Grid.RowCellStyleEventHandler(Heppler.gridView1_RowCellStyle1);

4、设置列表数据状态的显示颜色

/// <summary>
        /// 设置状态的颜色样式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void gridView1_RowCellStyle2(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            if (e.Column.FieldName == "单据状态") {
                if (e.CellValue != null && e.CellValue.ToString() == "已完成") {
                    e.Appearance.BackColor = Color.Green;
                }
            }
            if (e.Column.FieldName == "结算状态")
            {
                if (e.CellValue != null && e.CellValue.ToString() == "已结算")
                {
                    e.Appearance.BackColor = Color.Red;
                }
            }
        }

在窗体下,是这样的调用的

    //列表日期显示格式
           
this.gridView_List.RowCellStyle += new
DevExpress.XtraGrid.Views.Grid.RowCellStyleEventHandler(Heppler.gridView1_RowCellStyle2);

5、设置dev gridView中自动列宽适应的属性

gridView_List.OptionsView.ColumnAutoWidth = false;