1

I have a DataTable that is created in code behind. I need to right justify the number column and I also want to bold the last column of the DataTable with a dollar sign in the beginning. I also need to put commas for the numbers. below is my cs page code:

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

below is my .aspx page code:

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

below is something, I want:

enter image description here

I want something this way so that its easier to add the columns:

1234.00
   1
   6
   7
   9
45609.00

If I apply this stylesheet:

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

This is how the numbers look like: enter image description here

I want something like this:

     14
      6
      6
  82500.00
      0
     10
     20
      0
     10
      0
rimi
  • 624
  • 5
  • 15
  • Does this answer your question? [.NET GridView - Can you right-align just one column?](https://stackoverflow.com/questions/5644097/net-gridview-can-you-right-align-just-one-column) – Heretic Monkey Jun 16 '21 at 21:51
  • See [Separate number with comma for thousands asp.net](https://stackoverflow.com/q/31572019/215552) for the other question in your question. – Heretic Monkey Jun 16 '21 at 21:56

1 Answers1

2

To right justify only the second column, try the following:

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

You can use the same idea for first column to align in the left so it gives that space.

As for the commas, you can set the formatting like String.Format("{0:#,##0}", value)

Indrit Kello
  • 1,293
  • 8
  • 19
  • This just moves the numbers to the right hand side of the grid. They are still not aligned. – rimi Jun 16 '21 at 23:15
  • I updated my answer, how does it look right now? – Indrit Kello Jun 17 '21 at 08:16
  • I updated my original post. Basically, I want the numbers to be aligned so that its easier to add. I want 6 to be right above 82500.00 before the decimal. I modified my original post so that its easier to see. – rimi Jun 17 '21 at 08:25
  • Then you can try using the same formatting for all with .00 in the end. – Indrit Kello Jun 17 '21 at 08:28
  • Is it possible to do this alignment without putting .00 at the end for all numbers. I was thinking can this be resolved using string.format – rimi Jun 17 '21 at 08:49
  • Well you can also format the string with spaces in the right like `"${value} "` when it's a full number – Indrit Kello Jun 17 '21 at 09:21