如何在GridView将数字显示成金额格式或自定义格式呢?例如数字是123456.78,GRIDVIEW能否显示成123,456.78格式?
要自定义输出的内容,就只能使用自定义的模板.可以调用ToString()方法输出自定义格式的数据。然后使用ItemTemplate绑定到对应的列。最后通过<%#%>调用对应的函数来格式化显示的数据。
*.aspx代码如下:

<asp:GridView ID="GridView1" runat="server" OnPreRender="GridView1_PreRender" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" AutoGenerateColumns="False">
            
<Columns>
                
<asp:TemplateField>
                
<ItemTemplate>
                
<%# toNumberGroup((int)Eval("c1")) %>
                
</ItemTemplate>
                
</asp:TemplateField>
            
</Columns>
        
        
</asp:GridView>

*.aspx.cs代码如下:
public string toNumberGroup(int i)
    {
        
return i.ToString("n", System.Globalization.CultureInfo.CurrentUICulture.NumberFormat);
    }

protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt 
= new DataTable();
        dt.Columns.Add(
"c1",typeof(int));
        dt.Columns.Add(
"c2");
        dt.Rows.Add(
new object[]{11111,2});
        dt.Rows.Add(
new object[]{222222,2});

        
this.GridView1.DataSource = dt;
        
this.GridView1.DataBind();
    }



相关文章:

  • 2021-05-24
  • 2021-04-03
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-20
  • 2022-12-23
  • 2021-09-23
  • 2021-12-13
  • 2022-12-23
  • 2021-10-31
  • 2022-12-23
相关资源
相似解决方案