My datagrid doesn't show data in rows despite that I added data. Car is list of id of cars.
DataGridTextColumn col1 = new DataGridTextColumn();
myDataGrid.Columns.Add(col1);
col1.Binding = new Binding("id");
col1.Header = "ID";
foreach (Car carr in car)
{
myDataGrid.Items.Add(carr.ToString());
}
My xaml file
<DataGrid x:Name="myDataGrid"
HorizontalAlignment="Left"
Height="300"
Width="600"
VerticalAlignment="Top"
IsReadOnly="True"
RenderTransformOrigin="0.52,0.47"
Margin="100,82,0,0"
FontSize="15" />
Set the ItemSource property of the DataGrid:
myDataGrid.ItemSource = car;
See https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.datagrid?view=netframework-4.8
for more information.
OR You can use xaml for custom columns:
<DataGrid x:Name="CARS" ItemsSource="{Binding Path=Car}" IsReadOnly="True" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding Path=IdPropertyOfCarObject}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
And in code behind set the data context to the class that has a list of car objects.