2008-11-19 27 views
0

我想根据用户选择的货币组合框的货币更改我的DataGridView上的定价列。以编程方式更改DataGridView的文化?

目前,价格栏格式设置为“C2”。这默认看起来像“$ 1.00”。

但是,如果我的用户将货币换成大英镑,我想显示大英镑(“£”)而不是美元符号(“$”),所以最终结果将是£1.00。

有关如何更改DataGridView文化的任何建议?

在此先感谢!

+0

我加了标签asp.net请纠正我,如果我错了 – cgreeno 2009-01-28 11:54:07

回答

1

您正在寻找System.Globalization。有不同的选择一堆...

如果你只想改变它为特定的元素:

//Label example but theory is the same 
    [CultureInfo][2] ci = new CultureInfo("en-GB"); 
    double myMoney = 100.00; 
    this.Label1.Text = myMoney.ToString("C2", ci); 

如果你想改变它的一切那么你可以

 //Will format everything 
    string strCulture = "en-GB";//Session["culture"].ToString(); 
    [CultureInfo][3] ci = new CultureInfo(strCulture); 
    Thread.CurrentThread.[CurrentCulture][4] = ci ; 
    Thread.CurrentThread.[CurrentUICulture][5] = ci; 
    double myMoney = 100.00; 
    this.Label1.Text = myMoney.ToString("C2"); 

在DataGird中,如果您尝试格式化数据绑定行,您需要挂钩到onDataBound事件并重新格式化,因为我不相信您可以将参数传递为:DataFormatString =“{0:c, en-GB}

喜欢的东西所以应该做的伎俩(未测试)

protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     //Define CultureInfo in page scope just put in example for reference 
     [CultureInfo][6] ci = new CultureInfo("en-GB"); 
     if (e.Row.RowType == DataControlRowType.DataRow) 
      ((Label)e.Row.FindControl("myMoney")).Text.ToString("C2", ci); 
    } 

OR

如果从您可以explicatlly设置DataTable Cultureinfo

CultureInfo ci = new CultureInfo("en-GB"); 
myTable.Locale = ci; 

如果你是一个DataTable绑定寻找系统广泛的文化支持(我不认为你是但值得一提),那么你可以看看使用resource files

Simple example是:

ResourceManager rm = ResourceManager.CreateFileBasedResourceManager("resource", "path to resouce files", null); 
this.Label1.Text = rm.GetString("name");