2013-03-21 56 views
1

這是我的代碼格式的標籤爲###,###,###。##從SQL查詢

SqlConnection sqlConn = new SqlConnection(MyClass.GlobalConn()); 
sqlConn.Open(); 

try 
{ 

    string cmdStr = "Select CONVERT (char(12), CONVERT(money,sum(ProductAmt))),CONVERT (char(12), CONVERT(money,sum(ServiceAmt))) from MyTable"; 
    SqlCommand cmd = new SqlCommand(cmdStr, sqlConn); 

    SqlDataReader dR; 
    dR = cmd.ExecuteReader(); 

    while (dR.Read()) 
    { 
     label1.Text = (dR[0].ToString()); 
     label1.Text = (dR[1].ToString()); 
    } 



} 

Label1label2輸出格式########.## 我希望它是在###,###,###.##格式中,我怎樣才能使它成爲這種格式?

我有錯誤,當我嘗試提前label1.Text = (dR[0].ToString("N2"))

感謝。

+0

什麼樣的錯誤發生?請編輯 – 2013-03-21 06:45:40

+0

也請嘗試使用Console.out.WriteLine來「回聲」變量的內容。 – 2013-03-21 06:47:24

回答

0

這裏,將工作的方法:

public static string GetCurrencyString(string currString) 
{ 
    decimal moneyvalue; 
    if (Decimal.TryParse(currString, out moneyvalue)) 
    { 
     return String.Format("{0:C}", moneyvalue); 
    } 
    else 
    { 
     return null; 
    } 
} 

這樣稱呼它:

label1.Text = GetCurrencyString(dR[0].ToString()) 
+0

,非常感謝! – 2013-03-21 07:44:56

+0

不客氣!樂意效勞! – jordanhill123 2013-03-21 07:45:35