[WinForm][DevExpress][GridControl]CustomColumnDisplayText Helper

在实际开发中,有时候需要对GridControl中列值进行转义,譬如1转义成完成等等,一般在诸如CustomColumnDisplayText事件中能够轻松完成,为了提高代码复用性,所以对CustomColumnDisplayText进行包装

关键代码:

        /// <summary>
        /// CustomColumnDisplayText Helper
        /// </summary>
        /// <param name="girdview">GridView</param>
        /// <param name="fieldNameHandler">委托</param>
        /// <param name="dispalyTextHandler">展现文字</param>
        /// <param name="e">CustomColumnDisplayTextEventArgs</param>
        public static void CusColDisplayTextHelper(this GridView girdview, Predicate<string> fieldNameHandler, Func<object, string> dispalyTextHandler, CustomColumnDisplayTextEventArgs e)
        {
            if (fieldNameHandler(e.Column.FieldName))
            {
                e.DisplayText = dispalyTextHandler(e.Value);
            }
        }
        /// <summary>
        /// CustomColumnDisplayText Helper
        /// </summary>
        /// <param name="girdview">GridView</param>
        /// <param name="valueHandler">委托</param>
        /// <param name="dispalyTextHandler">委托</param>
        /// <param name="e">CustomColumnDisplayTextEventArgs</param>
        public static void CusColDisplayTextHelper(this GridView girdview, Func<object, Type, bool> valueHandler, Func<object, string> dispalyTextHandler, CustomColumnDisplayTextEventArgs e)
        {
            if (valueHandler(e.Value, e.Value.GetType()))
            {
                e.DisplayText = dispalyTextHandler(e.Value);
            }
        }
        /// <summary>
        ///CustomColumnDisplayText Helper
        /// </summary>
        /// <param name="girdview">GridView</param>
        /// <param name="valueHandler">委托</param>
        /// <param name="curdispalyText">展现文字</param>
        /// <param name="e">CustomColumnDisplayTextEventArgs</param>
        public static void CusColDisplayTextHelper(this GridView girdview, Func<object, Type, bool> valueHandler, string curdispalyText, CustomColumnDisplayTextEventArgs e)
        {
            if (valueHandler(e.Value, e.Value.GetType()))
            {
                e.DisplayText = curdispalyText;
            }
        }

代码使用:

        private void gvLampConfig_CustomColumnDisplayText(object sender, CustomColumnDisplayTextEventArgs e)
        {
            gvLampConfig.CusColDisplayTextHelper(name => name.Equals("LampViDirection"), value => value.ToInt(1) == 1 ? "正向" : "反向", e);
            gvLampConfig.CusColDisplayTextHelper(name => name.Equals("LampWorkStatus"), TranLampWorkType, e);
            gvLampConfig.CusColDisplayTextHelper(name => name.Equals("CTUChNo"), value => string.Format("第{0}回路", value), e);
            gvLampConfig.CusColDisplayTextHelper(name => name.Equals("LampPhase"), TranLampPhase, e);
            gvLampConfig.CusColDisplayTextHelper(name => name.Equals("LampDeviceType"), TranLampDeviceType, e);
            gvLampConfig.CusColDisplayTextHelper(name => name.Equals("LampPower"), value => string.Format("{0} W", value), e);
            gvLampConfig.CusColDisplayTextHelper(name => name.Equals("LampIntensity"), value => string.Format("{0} %", value), e);
            gvLampConfig.CusColDisplayTextHelper((value, type) => value.ToInt(-1) == -1 && type == typeof(Int32), "不修改", e);
        }
        private string TranLampDeviceType(object type)
        {
            int _type = type.ToInt(-1);
            if (_type == 1) return "钠灯";
            if (_type == 2) return "LED灯";
            if (_type == 3) return "无极灯";
            return "--";
        }
        private string TranLampPhase(object type)
        {
            int _type = type.ToInt(-1);
            if (_type == 0) return "未知";
            if (_type == 1) return "A相";
            if (_type == 2) return "B相";
            if (_type == 3) return "C相";
            return "--";
        }
        private string TranLampWorkType(object type)
        {
            int _type = type.ToInt(-1);
            if (_type == 0) return "关闭但不删除";
            if (_type == 1) return "启用";
            if (_type == 2) return "删除";
            return "--";
        }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }代码效果:

希望有所帮助!

[WinForm][DevExpress][GridControl]CustomColumnDisplayText Helper,布布扣,bubuko.com

时间: 2024-10-26 09:50:30

[WinForm][DevExpress][GridControl]CustomColumnDisplayText Helper的相关文章

C# - Winform - DevExpress - GridControl 任意条件控制Row背景色。

代码: private void gvSendConfirm_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)        {            string sCurrTime=DbHelperSql.GetSingle("Select GetDate() DateNow").ToString();            DateTime dtCu

[WinForm][DevExpress]自定义GridControl中按钮文字内容

最近项目开发中,使用到了GridControl的FindPanel,这样可以很好的对数据进行筛选,可是所展现的按钮文字是英文,如图: 那怎么定义两个按钮问题,以符合项目需求了?经过一番搜索发现利用GridLocalizer可以很好实现: 核心代码: public class BuilderGridLocalizer : GridLocalizer { Dictionary<GridStringId, string> CusLocalizedKeyValue = null; /// <su

DevExpress GridControl 按扭列的操作

为Winform 中的DevExpress GridControl某列添加按扭列,定义全局变量 RepositoryItemButtonEdit m_OperateFoodBtn = new RepositoryItemButtonEdit(); RepositoryItemButtonEdit m_OperateClothBtn = new RepositoryItemButtonEdit(); 设置相关属性方法 private void SetEditBtn(RepositoryItemBu

DevExpress GridControl 使用方法技巧 总结 收录整理

一.如何解决单击记录整行选中的问题 View->OptionsBehavior->EditorShowMode 设置为:Click 二.如何新增一条记录 (1).gridView.AddNewRow() (2).实现gridView_InitNewRow事件 三.如何解决GridControl记录能获取而没有显示出来的问题 gridView.populateColumns(); 四.如何让行只能选择而不能编辑(或编辑某一单元格) (1).View->OptionsBehavior->

DevExpress GridControl使用方法

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 8

[DevExpress]GridControl 根据RowIndex和VisibleColumnsIndex来获取单元格值

关键代码: /// <summary> /// 根据rowIndex和visibleColumnsIndex来获取单元格可见值 /// </summary> /// <param name="view">GridView</param> /// <param name="rowIndex">rowIndex</param> /// <param name="visibleColu

实现DevExpress GridControl 只有鼠标双击后才进行修改数据

1. 实现DevExpress GridControl 只有鼠标双击后才进行修改数据:修改GridView.OptionsBehavior.EditorShowMode属性为Click 2. 实现DevExpress GridControl 只有鼠标双击后才进行修改数据:修改GridView.OptionsBehavior.EditorShowMode属性为MouseDownFocused 然后实现下面代码: private void gridView1_MouseDown(object sen

[Devexpress]GridControl 绑定DataSource时GridView列不变化

Devexpress  GridControl 绑定DataSource时GridView列不变化 1.使用VS自带的DataGridView替代 2.数据绑定以后调用 gridView.PopulateColumns()  函数 重新创建列 gcSummary.DataSource = _dtData; gvSummary.PopulateColumns();

[DevExpress]GridControl 删除所有行

关键代码: /// <summary> /// 删除全部行 /// </summary> /// <param name="gridView">GridView</param> public static void ClearRows(this GridView gridView) { bool _mutilSelected = gridView.OptionsSelection.MultiSelect;//获取当前是否可以多选 if (