question

seihyungoh avatar image
0 Votes"
seihyungoh asked seihyungoh edited

[wpf] bind datatable to datagrid

Hello,

I have a datatable like this:

  1. property
    private DataTable amortable;
    public DataTable Amortable
    {
    get => amortable;
    set => SetProperty(ref amortable, value);
    }

  2. insert data into 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));
             //add row
             for (int i = 0; i < Convert.ToInt32(off.Terms)*12; i++)
             {
                 DataRow row = Amortable.NewRow();
                 row["Balance"] = Convert.ToDouble(off.LoanAmt);
                 row["Terms"] = i+1;
                 row["Payment"] = Convert.ToDouble(off.Payment);
                 row["Interest"] = Convert.ToDouble(off.APR) / 12 / 100 * Convert.ToDouble(row["Balance"]);
                 row["Principal"] = Convert.ToDouble(row["Payment"]) - Convert.ToDouble(row["Interest"]);
                 if (i > 0)
                 {
                     row["Balance"] = Convert.ToDouble(row["Balance"]) - Convert.ToDouble(row["Principal"]);
                 }
                 Amortable.Rows.Add(row);
    

  3. datagrid(in XAML)

      <DataGrid x:Name="AmortizTable" ItemsSource="{Binding Amortable}"  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" IsReadOnly="True"/>
                 <DataGridTextColumn Header="Principal" IsReadOnly="True"/>
                 <DataGridTextColumn Header="Interest" IsReadOnly="True"/>
                 <DataGridTextColumn Header="Payment" IsReadOnly="True" />
                 <DataGridTextColumn Header="Balance" IsReadOnly="True" />
             </DataGrid.Columns>
         </DataGrid>
    

after running above code, I couldn't see data in datagrid.
116931-ice-screenshot-20210722-120043.png



if someone pick my mistake in this code, I would be very appreciated.

thanks,

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

Viorel-1 avatar image
0 Votes"
Viorel-1 answered seihyungoh edited

Try specifying the binding, for example:

<DataGridTextColumn Header="Term" Binding="{Binding Terms}" IsReadOnly="True"/>

But if you set AutoGenerateColumns="True" to your data grid, then the columns and rows will appear automatically.

· 1
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.

Thanks for picking my mistake!

0 Votes 0 ·