WPF中的Datagrid显示空行和列

时间:2014-03-28 15:01:36

标签: c# datagrid datatable wpf-controls

我在WPF应用程序中有一个小数据表。我想将它绑定到DataGrid,但datagrid显示空行.Kindly help

代码: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;
    }

代码:XAML

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

1 个答案:

答案 0 :(得分:3)

尝试设置AutoGenerateColumns="true"并检查。

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