[wpf] bind datatable to datagrid

c00012 716 Reputation points
2021-07-22T03:21:22.337+00:00

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 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

Accepted answer
  1. Viorel 117.1K Reputation points
    2021-07-22T03:39:17.127+00:00

    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.


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.