码迷,mamicode.com
首页 > 其他好文 > 详细

Dexpress 中 grid的使用

时间:2015-08-09 15:21:29      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

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

 

Dexpress 中 grid的使用

标签:

原文地址:http://www.cnblogs.com/xuzai/p/4715111.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!