How to load DataTable into a DataGrid?

Posted on

Question :

I have DataTable loaded with information in a WPF application.

I would like to get this DataTable and load it into a DataGrid .
How can I do this the easy way?

DataGrid in XAML:

<Grid>
    <Button Content="Pedidos&#xD;&#xA;" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="91" Click="Button_Click" Height="27"/>
    <DataGrid Name="dataGrid" HorizontalAlignment="Left" Margin="10,42,0,0" VerticalAlignment="Top" Height="268" Width="558"/>
</Grid>

    

Answer :

There are several ways to “populate” a DataGridView with a DataTable .

Here are two simple ways to do it.

Using a BindingSource :

BindingSource bs = new BindingSource();
bs.DataSource = seuDataTablePopulado;
datagrid.DataSource = bs;

“Playing” DataTable direct on the grid:

datagrid.DataSource = seuDataTable;

Between these two forms I’d rather use BindingSource for the filter options I have later.

    

Leave a Reply

Your email address will not be published. Required fields are marked *