Dexpress 中 grid的使用

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;

 

posted @ 2015-08-09 14:22  蚂蚁金服官方博客  阅读(854)  评论(0编辑  收藏  举报