C# 将布尔值显示为是或否

C# 将布尔值显示为是或否,c#,boolean,oledbdatareader,C#,Boolean,Oledbdatareader,我正在查询数据库,并将值分配给我序列化并显示在报告中的对象 问题是bool变量在报告中显示为true或false。如何将值显示为是或否 这是我的课 public class ProductReportView { public int Count { get; set; } public string ProductCode { get; set; } public string ProductTitle { get; set; } public string P

我正在查询数据库,并将值分配给我序列化并显示在报告中的对象

问题是bool变量在报告中显示为true或false。如何将值显示为是或否

这是我的课

public class ProductReportView
{
    public int Count { get; set; }
    public string ProductCode { get; set; }
    public string ProductTitle { get; set; }
    public string Producer { get; set; }

    public bool VideoOnDemand { get; set; }
    public bool PreviewScreen { get; set; }
    public bool QualityCheck { get; set; }
    public bool Archive { get; set; }
}
这就是我分配值的方式

OleDbDataReader dbreader = cmd.ExecuteReader();

while (dbreader.Read())
{
     Console.WriteLine("Record " + totalCount++);
     ProductReportView rep = new ProductReportView();
     rep.Count = ++totalCount;
     rep.ProductCode = (string)dbreader["CODE"];
     rep.ProductTitle = (string)dbreader["TITLE"];
     rep.Producer = (string)dbreader["PRODUCER"];
     rep.VideoOnDemand = (bool)dbreader["VideoOnDemand"];
     rep.PreviewScreen = (bool)dbreader["PreviewLibraryChecked"];
     rep.QualityCheck = (bool)dbreader["QualityCheck"];
     rep.Archive = (bool)dbreader["Archive"];
     lst.Add(rep);
}

这些值基于选中和取消选中的复选框dVideoOnDemand、PreviewScreen QualityCheck、Archive

在对象中存储值时使用三元运算符

rep.VideoOnDemand = (bool)dbreader["VideoOnDemand"] ? "Yes" : "No";  
并将VideoOnDemand作为字符串


对于其他需要“是/否”的变量,请使用相同的方法。

您不需要说明如何“报告”

这有用吗

   Control.Text = rep.VideoOnDemand ? "Yes" : "No";

您也可以在Sql查询中执行此操作

例如

挑选

案例视频点播 当我说“是” 否则“不” 以“VideoOnDemand”结尾


从tblxyz来看,在将值存储到对象中的过程中进行更改确实是个坏主意。因此,在网格中的C级别执行此操作

Control.Text = rep.VideoOnDemand ? "Yes" : "No";

我的方法有4个简单的属性,提供了可重用性和清理代码:

public class ProductReportView
{
    public int Count { get; set; }
    public string ProductCode { get; set; }
    public string ProductTitle { get; set; }
    public string Producer { get; set; }

    public bool VideoOnDemand { get; set; }
    public bool PreviewScreen { get; set; }
    public bool QualityCheck { get; set; }
    public bool Archive { get; set; }

    private string toYesNo(bool b)
    {
        return b ? "Yes" : "No";
    }

    public string VideoOnDemandString
    {
        get { return this.toYesNo(this.VideoOnDemand); }
    }

    public string PreviewScreenString
    {
        get { return this.toYesNo(this.PreviewScreen); }
    }

    public string QualityCheckString
    {
        get { return this.toYesNo(this.QualityCheck); }
    }

    public string ArchiveString
    {
        get { return this.toYesNo(this.Archive); }
    }
}
此代码可以在您的所有应用程序中重用,而无需重复yes、no、yes、no等

最后一点建议:bool应该存储在bool属性中,字符串应该存储在string属性中


不要持久化转换为字符串的布尔值:没有任何意义。

replicate of-在报告中显示look Where和how?我认为将其保存为字符串不是很好的做法。将来仍然将其存储为布尔值可能会很有用。这是一个演示问题。
public class ProductReportView
{
    public int Count { get; set; }
    public string ProductCode { get; set; }
    public string ProductTitle { get; set; }
    public string Producer { get; set; }

    public bool VideoOnDemand { get; set; }
    public bool PreviewScreen { get; set; }
    public bool QualityCheck { get; set; }
    public bool Archive { get; set; }

    private string toYesNo(bool b)
    {
        return b ? "Yes" : "No";
    }

    public string VideoOnDemandString
    {
        get { return this.toYesNo(this.VideoOnDemand); }
    }

    public string PreviewScreenString
    {
        get { return this.toYesNo(this.PreviewScreen); }
    }

    public string QualityCheckString
    {
        get { return this.toYesNo(this.QualityCheck); }
    }

    public string ArchiveString
    {
        get { return this.toYesNo(this.Archive); }
    }
}