【问题标题】:how to right justify and format the column in a gridview如何在gridview中正确对齐和格式化列
【发布时间】:2021-06-16 20:50:12
【问题描述】:

我有一个在后面的代码中创建的 DataTable。我需要右对齐数字列,并且我还想在开头用美元符号加粗 DataTable 的最后一列。我还需要为数字加上逗号。下面是我的cs页面代码:

    DataTable dt = new DataTable("Table");
    dt.Columns.Add(new DataColumn("Title"));              
    dt.Columns.Add(new DataColumn("Number));
    dt.Rows.Add("Number", 12.3);
    dt.Rows.Add("Pages", 45789);
    dt.Rows.Add("requirements", 23456);
    dt.Rows.Add("Price", 12);
    dt.Rows.Add("Property", 25);
    dt.Rows.Add("test1", 0);
    dt.Rows.Add("Total", total);
    grdCalculate.DataSource = dt;
    grdCalculate.DataBind();

下面是我的 .aspx 页面代码:

 <asp:GridView ID="grdCalculate" runat="server" GridLines="Horizontal" CssClass="grid"></asp:GridView>

下面是我想要的东西:

我想要这样的东西,以便更容易添加列:

1234.00
   1
   6
   7
   9
45609.00

如果我应用这个样式表:

 table tr td:nth-child(2)
{
    text-align:right !important;   
}

这是数字的样子:

我想要这样的东西:

     14
      6
      6
  82500.00
      0
     10
     20
      0
     10
      0

【问题讨论】:

标签: c# asp.net webforms


【解决方案1】:

要仅右对齐第二列,请尝试以下操作:

table tr td:nth-child(2)
{
    text-align:right !important;   
}
 

您可以使用相同的想法将第一列与左侧对齐,以便留出空间。

逗号的格式可以设置成String.Format("{0:#,##0}", value)

【讨论】:

  • 这只是将数字移动到网格的右侧。它们仍然没有对齐。
  • 我更新了我的答案,现在看起来怎么样?
  • 我更新了我原来的帖子。基本上,我希望数字对齐,以便更容易添加。我希望 6 在小数点前正好高于 82500.00。我修改了我原来的帖子,以便更容易看到。
  • 那么你可以尝试使用相同的格式对所有最后加上 .00 的格式。
  • 是否可以在所有数字的末尾不加 .00 的情况下进行这种对齐。我在想这可以使用 string.format 解决吗
猜你喜欢
  • 2010-10-15
  • 2018-12-15
  • 1970-01-01
  • 2021-11-25
  • 1970-01-01
  • 2019-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多