我的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;
}
DataGrid 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" />
答案 0 :(得分:0)
将数据网格的AutoGenerateColumns设置为true