0

我的 WPF 应用程序中的数据网格不显示 Excel 表中的数据。它只显示水平线。

private void btUpload_Click(object sender, RoutedEventArgs e)
    {

        // Configure open file dialog box 
        Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
        // Filter by Excel Worksheets
        dlg.Filter = "Excel Worksheets|*.xls";

        dlg.ShowDialog();

        // Show open file dialog box 
        Nullable<bool> result = dlg.ShowDialog();

        //string FileName = "dlg"; // From the dialog box.
   // Process open file dialog box results 

       if (result == true)
        {
           string path = System.IO.Path.GetFullPath("dlg");

            string query = "SELECT * FROM [code$]";

            OleDbConnection conn = new OleDbConnection();

            conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dlg.FileName + ";Extended Properties=" + "\"Excel 12.0 Xml;HDR=YES;IMEX=1\"";

            OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn);

            //DataSet dataSet = new DataSet();
            DataSet dsCodes = new DataSet();
            adapter.Fill(dsCodes);
        dgCodeDisp.ItemsSource = dsCodes.DefaultViewManager;
}

数据网格 xaml 代码:

<DataGrid x:Name="dgCodeDisp" Grid.Column="1" HorizontalAlignment="Left" Height="141" Margin="260,230,0,0" VerticalAlignment="Top" Width="240" SelectionChanged="DataGrid_SelectionChanged" Grid.ColumnSpan="3" Grid.RowSpan="2" />
4

1 回答 1

0

将数据网格的 AutoGenerateColumns 设置为 true

于 2013-10-03T07:22:35.170 回答