سؤال

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}" />
هل كانت مفيدة؟

المحلول

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}" />
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top