2013-02-12 133 views
-1

我正在查詢數據庫並將值分配給一個對象,我將其序列化並顯示在報告中。將bool值顯示爲Yes或No

事情是布爾變量在報告中顯示爲真或假。如何讓值顯示爲「是」或「否」。

這是我的課

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); 
} 

值是基於被選中和未選中(VideoOnDemand,PreviewScreen QualityCheck,歸檔)

複選框值
+0

重複的http://stackoverflow.com/questions/5632920/how-to-generically-format-a-boolean-to-a-yes-no-string - 看看 – Anthill 2013-02-12 10:45:49

+1

'顯示在報告中'哪裏如何? – 2013-02-12 10:46:44

回答

-1

使用三元運算符,同時將您的值存儲在對象中

rep.VideoOnDemand = (bool)dbreader["VideoOnDemand"] ? "Yes" : "No"; 

而且使VideoOnDemandstring

public string VideoOnDemand { get; set; } 

使用相同的方法爲你需要的是變量/ NO

+6

我不認爲這是非常好的做法,將其保存爲字符串。將來仍然可以將它作爲布爾值存儲起來。這是一個演示問題。 – 2013-02-12 10:49:06

4

你不說你怎麼是「報告」的休息......

這有幫助嗎?

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

你也可以在你的Sql Query中這樣做。

例如,

選擇

情況VideoOnDemand 當1然後 'YES' 別的 'NO' 端爲 'VideoOnDemand'

從tblxyz

2

存儲在對象中的值時更改是一個真正的餿主意。因此,無論是在C#級網格

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

我的做法,有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); } 
    } 
} 

這個代碼可以在所有應用程序,而不重複重複使用「是」,「否」,「是」,「否」等。

最終建議:bools應存儲在bool屬性中,字符串應存儲在字符串屬性中。

不要將bool值轉換爲字符串:沒有任何意義。

相關問題