欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

DevExpress中GridControl列转义的实现方法

程序员文章站 2023-12-17 22:10:10
在一些项目的实际开发过程中,我们有时候需要对gridcontrol中列值进行转义,譬如1转义成“完成”等等,一般在诸如customcolumndisplaytext事件中能...

在一些项目的实际开发过程中,我们有时候需要对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 "--";
}

代码运行效果如下所示:

DevExpress中GridControl列转义的实现方法

上一篇:

下一篇: