我尝试使用ado.net从数据库填充wpf listview。我有这段代码:
private void Window_loaded(object sender, RoutedEventArgs e)
{
ShowData();
}
public void ShowData()
{
SqlConnection con = new SqlConnection(@"Data Source=mySource;Initial Catalog=dbName;Integrated Security=True");
try
{
con.Open();
SqlCommand comm = new SqlCommand("select book.ID_book, book.book, book.ID_autor, book.genre from book", con);
DataTable dt = new DataTable();
SqlDataAdapter data = new SqlDataAdapter(comm);
data.Fill(dt);
ListView1.DataContext = dt.DefaultView;
con.Close();
}
catch
{
MessageBox.Show("error connection to database", "", MessageBoxButton.OK);
}
}
我做错了什么?这不是我的连接错误
这是我的listview xaml:
<ListView Canvas.Left="243" Canvas.Top="41" Canvas.Right="0"
Width="262"
Name="ListView1" ItemsSource="{Binding DataContext}"
MinWidth="250" MinHeight="100" Height="182" Margin="1"
>
<ListView.View>