GridControl 控制列中的控件显示

一、需求描述

根据条件判断Checkbox的显示,先上个图,

类似Demo中的这个,因为不能控制文本的显示,所以需对该列的其它事件做些处理,

 二、解决方案

1、添加GridControl上需要的控件

在设计器上添加RepositoryItemCheckEdit、RepositoryItemFontEdit、RepositoryItemMemoEdit 三种字段类型。

再分别给这三个字段添加相应的事件:

RepositoryItemCheckEdit 添加 QueryCheckStateByValue事件;

RepositoryItemFontEdit 添加 CustomDisplayText事件, 设置ReadOnly 为true;

RepositoryItemMemoEdit 使用此控件,主要是用来换行,要保证GridView1.OptionsView.RowAutoHeight = true。

2、设置GridControl 和 GridView

添加事件CustomRowCellEdit。

设置属性OptionsView.ShowGroupPanel = false;OptionsView.ShowIndicator = false;OptionsView.RowAutoHeight = true;

3、相应代码

private void repositoryItemFontEdit1_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            e.DisplayText = string.Empty;
        }

        private void repositoryItemCheckEdit1_QueryCheckStateByValue(object sender, DevExpress.XtraEditors.Controls.QueryCheckStateByValueEventArgs e)
        {
            string val = "";
            if (e.Value != null)
            {
                val = e.Value.ToString();
            }
            else
            {
                val = "False";//默认为选中
            }
            switch (val)
            {
                case "True":
                    e.CheckState = CheckState.Checked;
                    break;
                case "False":
                    e.CheckState = CheckState.Unchecked;
                    break;
                case "Yes":
                    goto case "True";
                case "No":
                    goto case "False";
                case "1":
                    goto case "True";
                case "0":
                    goto case "False";
                case "Y":
                    goto case "True";
                case "N":
                    goto case "False";
                default:
                    e.CheckState = CheckState.Unchecked;
                    break;
            }
            e.Handled = true;
        }

private void gvGridView_CustomRowCellEdit(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e)
        {
            GridView gv = sender as GridView; 
            string syDesc = gv.GetRowCellValue(e.RowHandle, gv.Columns["FieldValueCol"]).ToString();
            switch (e.Column.FieldName)
            {
                case "FieldName":
                    if (syDesc == "错误")
                        e.RepositoryItem = repositoryItemFontEdit1;
                    else
                    { 
                        e.RepositoryItem = repositoryItemCheckEdit1;
                    }

                    break;
            }
        }
View Code
原文地址:https://www.cnblogs.com/windy2008/p/4883122.html