I use a DataTable as source for my DataGrid:
dt.Columns.Add("Update?", typeof(Boolean));
dt.Columns.Add("Emulator", typeof(String));
dt.Columns.Add("Path", typeof(String));
...
dataGrid1.ItemsSource = dt.DefaultView;
The XAML code to the DataGrid:
<DataGrid AutoGenerateColumns="True" Height="133" HorizontalAlignment="Left" Margin="0,81,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="688" >
<DataGrid.Columns>
<DataGridCheckBoxColumn CanUserResize="False" Header="Update?" />
<DataGridTextColumn CanUserResize="False" Header="Emulator" IsReadOnly="False"/>
<DataGridTextColumn CanUserResize="False" Header="Path" IsReadOnly="False"/>
</DataGrid.Columns>
</DataGrid>
So if the DataTable is empty I got an empty DataGrid showing the three headers names.
If the DataTable contains items the DataGrid gets extra columns instead of filling the already existing ones.
What can I do that the DataGrid fills up its existing columns?
set AutoGenerateColumns="False"
if you set this property as true
the columns will be created automatically
And also you need to set the binding , sample code :
<DataGrid Name="DG1" AutoGenerateColumns="False" ItemsSource="{Binding}">
<DataGrid.Columns>
<DataGridCheckBoxColumn Header="Online Order?" IsThreeState="True" Binding="{Binding OnlineOrderFlag}" />
</DataGrid.Columns>
</DataGrid>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With