[wpf-datagrid] numeric format doesn't work...

c00012 716 Reputation points
2021-07-26T02:28:02.89+00:00

Hello,

I have an amortization example for Personal Loan like this;

117666-ice-screenshot-20210721-212616.png

I want to implement this using datagrid. so I wrote following code:

  1. XAML
     <DataGrid x:Name="AmortizTable" ItemsSource="{Binding Amortable}" AutoGenerateColumns="False"  Grid.Row="1">  
        <DataGrid.Resources>  
            <Style TargetType="DataGridCell">  
                <Setter Property="HorizontalAlignment" Value="Right"/>  
                <Setter Property="HorizontalContentAlignment" Value="Right"/>  
                <Setter Property="Width" Value="90"/>  
            </Style>  
            <Style TargetType="DataGridColumnHeader">  
                <Setter Property="HorizontalContentAlignment" Value="Center"/>  
                <Setter Property="HorizontalAlignment" Value="Center"/>  
                <Setter Property="Width" Value="90"/>  
            </Style>  
        </DataGrid.Resources>  
        <DataGrid.Columns>  
            <DataGridTextColumn Header="Term" Binding="{Binding Terms}" IsReadOnly="True"/>  
            <DataGridTextColumn Header="Principal" Binding="{Binding Principal}" IsReadOnly="True"/>  
            <DataGridTextColumn Header="Interest" Binding="{Binding Interest}" IsReadOnly="True"/>  
            <DataGridTextColumn Header="Payment" Binding="{Binding Payment}" IsReadOnly="True" />  
            <DataGridTextColumn Header="Balance" Binding="{Binding Balance}" IsReadOnly="True" />  
        </DataGrid.Columns>  
    </DataGrid>  
    
    1. Datatable
      //create datatable and add column  
      Amortable = new DataTable();  
      Amortable.Columns.Add("Terms", typeof(int));  
      Amortable.Columns.Add("Principal", typeof(double));  
      Amortable.Columns.Add("Interest", typeof(double));  
      Amortable.Columns.Add("Payment", typeof(double));  
      Amortable.Columns.Add("Balance", typeof(double));  
      
      //insert row at first row  
      DataRow row = Amortable.NewRow();  
      row["Balance"] = Math.Ceiling(Convert.ToDouble(off.LoanAmt));  
      row["Terms"] = 0;  
      row["Payment"] = 0.0;  
      row["Interest"] = 0.0;  
      row["Principal"] = 0.0;  
      Amortable.Rows.InsertAt(row, 0);  
      //add row   
      for (int i = 0; i <Convert.ToInt32(off.Terms) * 12; i++)  
      {  
          DataRow dataRow = Amortable.NewRow();  
          dataRow["Terms"] = i + 1;  
          dataRow["Payment"] = Math.Ceiling(Convert.ToDouble(off.Payment));  
          dataRow["Interest"] = Math.Ceiling(Convert.ToDouble(off.APR) / 12 / 100 * Convert.ToDouble(Amortable.Rows[i].Field<double>("Balance")));  
          dataRow["Principal"] = Math.Ceiling(Convert.ToDouble(dataRow["Payment"]) - Convert.ToDouble(dataRow["Interest"]));  
          dataRow["Balance"] = Math.Ceiling(Convert.ToDouble(Amortable.Rows[i]. Field<double>("Balance")) - Convert.ToDouble(dataRow["Principal"]));  
          if (Convert.ToDouble(dataRow["Balance"]) < 0.0)  
              dataRow["Balance"] = 0.0;  
          Amortable.Rows.Add(dataRow);  
      }  
      
      after running above code, I thought above code work, but I want to add thousand separators to the data in datagrid, so I add following as a test:
      row["Balance"] = Math.Ceiling(Convert.ToDouble(off.LoanAmt)).ToString("#,###")
      after running this code, I see this format doesn't work. How can I fix it? if someone give me a good advice, I would be very appreciated.

c00012

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,768 questions
0 comments No comments
{count} votes

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.