Dexpress 中 grid的使用

本文介绍了DevExpress Grid控件的多种实用操作,包括如何在Grid中加入Checkbox、ComboBox的初始化设置、单元格值的计算更新、单元格颜色的动态设置、自定义总计计算方式以及如何实现列的冻结等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

grid 中加入checkbox 

Run desiger

Columns

ColumnEdit中选择checkbox

此时我们要在后台手动指定一个数据列如下代码:

 dtRebatesReport.Columns.Add("MutiSelect", System.Type.GetType("System.Boolean")).DefaultValue = false;
            dtRebatesReport.Columns.Add("PAYMENTSTATE1", System.Type.GetType("System.Boolean")).DefaultValue = false;
            for (int i = 0; i < dtRebatesReport.Rows.Count; i++)
            {
                DataRow dr = dtRebatesReport.Rows[i];
                if (dr["PAYMENTSTATE"].ToString() == "0")
                {
                    dr["PAYMENTSTATE1"] = false;
                }
                else
                {
                    dr["PAYMENTSTATE1"] = true;
                }

            }

这里后台如果返回的是int数据,一定要手动转为bool  后台返回true不可用。

grid.GetRowCellValue(i,columnsName)    //取得指定单元格文件

grid.SetRowCellValue(i,columnsName,value)    //设定单元格传

如果gird中加入combox  找到combox名称。直接给值即可初始化

如下我们可以判断是否可编辑

 /// <summary>
        /// 行焦点发生改变时,是否可编辑,可删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gvInComeDetailReport_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            GridView gv = sender as GridView;
            if (e.FocusedRowHandle >= 0)
            {
                bool CanEdit = Convert.ToBoolean(gv.GetRowCellValue(e.FocusedRowHandle, this.gridColumn16));
                if (CanEdit)
                {
                    //已付 时不可编辑
                    this.gridColumn11.OptionsColumn.AllowEdit = false;
                    this.gridColumn12.OptionsColumn.AllowEdit = false;
                    this.gridColumn14.OptionsColumn.AllowEdit = false;
                    this.gridColumn15.OptionsColumn.AllowEdit = false;
                }
                else
                {
                    this.gridColumn11.OptionsColumn.AllowEdit = true;
                    this.gridColumn12.OptionsColumn.AllowEdit = true;
                    this.gridColumn14.OptionsColumn.AllowEdit = true;
                    this.gridColumn15.OptionsColumn.AllowEdit = true;
                }


            }
        }
View Code

如下我们在更改单元格的值 时可计算

 private void gvInComeDetailReport_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            //选择列值改变
            if (e.Column == this.gridColumn11)
            {
                if (!CommonUtils.isEmpty(CommonUtils.ObjectToString(e.Value)))
                {
                    //折扣金额
                    string str1=CommonUtils.ObjectToString(gvInComeDetailReport.GetRowCellValue(e.RowHandle, this.gridColumn9));
                    string str2 = CommonUtils.ObjectToString(e.Value);
                    string money = string.IsNullOrEmpty(str1) ? "0" : str1;
                    string SendMoney = string.IsNullOrEmpty(str2) ? "0" : str2;
                    decimal v1, v2 = 0;
                    decimal zkMoney = 0;
                    if (decimal.TryParse(money, out v1))
                    {
                        zkMoney = v1;
                    }
                    decimal CellMoney = 0;

                    if (decimal.TryParse(SendMoney, out v2))
                    {
                        CellMoney = v2;
                    }

                    gvInComeDetailReport.SetRowCellValue(e.RowHandle, this.gridColumn14,zkMoney+CellMoney);

                }
            }
        }
View Code


如下设置单元格颜色

        private void gvInComeDetailReport_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            GridView gv = sender as GridView;
            if (e.RowHandle >= 0)
            {
                bool CanEdit = Convert.ToBoolean(gv.GetRowCellValue(e.RowHandle, this.gridColumn16));
                if (CanEdit)
                {
                    e.Appearance.BackColor = Color.LightGray;
                }
                else
                {
                    //获取所在行指定列的值
                    string state = this.gvInComeDetailReport.GetRowCellValue(e.RowHandle, "MutiSelect").ToString();
                    //比较指定列的状态
                    if (state == "True")
                    {
                        e.Appearance.BackColor = Color.BlueViolet;//设置此行的背景颜色
                    }
                    else
                    {
                        e.Appearance.BackColor = Color.White;//设置此行的背景颜色
                    }
                
                }
               
            }
        }
View Code

合计各列数值

 decimal _customSum = 0;
        private void gvInComeDetailReport_CustomSummaryCalculate(object sender, DevExpress.Data.CustomSummaryEventArgs e)
        {
            GridView gridView = sender as GridView;
            DataRow dr = gridView.GetDataRow(e.RowHandle);
            if ("REBATEAMOUNT".Equals((e.Item as GridSummaryItem)) ||
                "ADJUSTMENTAMOUNT".Equals((e.Item as GridSummaryItem).FieldName) ||
                "ACTUALAMOUNT".Equals((e.Item as GridSummaryItem).FieldName)
                )
            {
               
                if (!CommonUtils.isEmpty(CommonUtils.ObjectToString(dr[(e.Item as GridSummaryItem).FieldName])))
                {
                    _customSum += Convert.ToDecimal(dr[(e.Item as GridSummaryItem).FieldName]);
                }
                e.TotalValue = _customSum;
            }

        }
View Code

设置冻结列(左冻结)
gridView1.Columns[0].Fixed= DevExpress.XtraGrid.Columns.FixedStyle.Left;

设某一列文字和标题局中显示
gridView1.Columns[0].AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
gridView1.Columns[0].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;

设置自动增加的行号,需要先添加给gridview添加事件CustomDrawRowIndicator

private void gridview_CustomDrawRowIndicator(object sender,                            DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
{
if (e.Info.IsRowIndicator && e.RowHandle >= 0)
e.Info.DisplayText = (e.RowHandle + 1).ToString();
}

 设置列宽

  this.gvNetAgentDailyReport.IndicatorWidth = Convert.ToInt32(Math.Ceiling(
                    CommonUtils.ObjectToString(this.dtNetAgentDailyReport.Rows.Count).Length * 7.1)) + 20;

 

转载于:https://www.cnblogs.com/xuzai/p/4715111.html

ExpressQuantumGrid_Suite_v5.0_Full_Source_Code_for_Delphi_7 补丁安装说明:1.如果正在运行Delphi_7,请先关闭之。2.运行ExpressQuantumGrid_Suite_v5.0_Full_Source_Code_for_Delphi_7控件包中的Setup.exe来进行正常安装。3.不要运行Delphi_7。将压缩包中以下二个文件:cxExtEditorsVCLD7.bpldclcxExtEditorsVCLD7.bpl解压到操作系统的System目录(注:W2K/WinXP中应为system32目录)中覆盖原文件。4.将压缩包中ExpressEditors Library 5目录解压到Program FilesDeveloper Express Inc下覆盖原安装目录; 将压缩包中ExpressPageControl 2目录解压到Program FilesDeveloper Express Inc下覆盖原安装目录; 将压缩包中ExpressQuantumGrid 5目录解压到Program FilesDeveloper Express Inc下覆盖原安装目录; 将压缩包中Bpl目录解压到BorlandDELPHI7Projects下覆盖原目录。5.运行Delphi_7,在Delphi_7中通过Tools->Environment Options->Library->Library path菜单路径打开Diredtories窗口。将以下路径:......Developer Express IncExpressDataControllerDelphi 7Lib......Developer Express IncXP Theme ManagerDelphi 7Lib......Developer Express IncCX LibraryDelphi 7Lib......Developer Express IncExpressEditors Library 5Delphi 7Lib......Developer Express IncExpressPageControl 2Delphi 7Lib......Developer Express IncExpressQuantumGrid 5Delphi 7Libreplace(替换)为:......Developer Express IncExpressDataControllerDelphi 7Sources......Developer Express IncXP Theme ManagerDelphi 7Sources......Developer Express IncCX LibraryDelphi 7Sources......Developer Express IncExpressEditors Library 5Delphi 7Sources......Developer Express IncExpressPageControl 2Delphi 7Sources......Developer Express IncExpressQuantumGrid 5Delphi 7Sources6. GOOD LUCK, 补丁安装成功!打开ExpressQuantumGrid 5Delphi 7Demos中的各个Demo,编译后可以运行!其中:ConvertGrid3Demo和ConvertGrid3MasterDetailDemo还必须同时安装“DevExpress ExpressQuantumGrid Suite v3.22 for Delphi7”,才行! ------- Done By QHB ------- Email: mylovelyqq@hotmail.com
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值