question

seihyungoh avatar image
0 Votes"
seihyungoh asked seihyungoh answered

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

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>
    
  2. 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-wpf
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

seihyungoh avatar image
0 Votes"
seihyungoh answered
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.