首页 > 代码库 > [WinForm][DevExpress][GridControl]CustomColumnDisplayText Helper
[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 "--"; }
<style type="text/css">.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; }</style>代码效果:
希望有所帮助!