Question

I have a small datatable in a WPF application. I want to bind it to the DataGrid ,but the datagrid shows blank row.Kindly help

Code : C#

    private void button1_Click(object sender, RoutedEventArgs e)
    {

        DataTable dt = new DataTable();
        dt.Columns.Add("aaa");
        dt.Columns.Add("bbb");
        dt.Rows.Add(1, 2);
        dt.Rows.Add(3, 4);
        int i = dt.Columns.Count;
        int rows = dt.Rows.Count;
        dataGrid1.CanUserAddRows = false;
        dataGrid1.ItemsSource = dt.DefaultView;
    }

Code : XAML

<DataGrid AutoGenerateColumns="False" Height="200" HorizontalAlignment="Left" Margin="136,108,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="200" ItemsSource="{Binding}" />
Was it helpful?

Solution

Try setting the AutoGenerateColumns="true" and check.

<DataGrid AutoGenerateColumns="true" Height="200" HorizontalAlignment="Left" Margin="136,108,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="200" ItemsSource="{Binding}" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top