搜索在整数列上自动过滤字符串的方法

问题描述 投票:0回答:1

我正在使用devexpress gridview进行车辆管理程序,并得到一个列,显示该行的数据是否被标记为已删除。在数据库中,它们标记为“1”表示活动,“11”表示已删除,因此我创建了一个枚举类:

public enum enmSystemstatus
{
    Created = 1, 
    Deleted = 11
}

这个事件用grid中的单词而不是数字标记它们:

private void gridView1_CustomColumnDisplayText(object sender, CustomColumnDisplayTextEventArgs e)
    {
        if (e.Column == colVehicle_Systemstatus && e.Value != null)
        {
            e.DisplayText = (int)e.Value == (int)enmSystemstatus.Created ? "Active" : "Deleted";
        }
    }

但是该程序也应该可以使用自动过滤行,但是如果我在systemstatus列的搜索框中输入一个字母,程序就会崩溃,因为无法转换这些值来调用异常:

“system.invalidcastexception指定的强制转换无效。”

这可能是因为数据库上的列是整数列,但将其更改为varchar不会影响某些内容

有人为此解决了吗?

提前致谢

c# winforms devexpress
1个回答
0
投票

DevExpress组件(例如GridControl)支持the wide range of annotation attributes,它允许您有效地将数据库中的整数值映射到特定的枚举成员,并为每个枚举成员提供有意义且可本地化的描述:

public enum DisplayStatus {
    [Display(Name = "ACTIVE")]
    Active= 1,
    [Display(Name = "DELETED")]
    Deleted = 11
}
public class Vehicle_DTO {
    [EnumDataType(typeof(DisplayStatus))]
    [Display(Name = "STATUS")]
    public int Status { get; set; }
}
// ...
gridControl1.DataSource = new List<Vehicle_DTO> {
    new Vehicle_DTO() { Status = 1 },
    new Vehicle_DTO() { Status = 11 },
    new Vehicle_DTO() { Status = 11 },
};

此自定义将应用于所有UI元素,包括过滤器,没有任何事件处理和错误: annotation attributes in action

© www.soinside.com 2019 - 2024. All rights reserved.