我想從.mdf數據庫文件中使用ListView顯示數據庫內容。這裏是我使用的代碼塊:ListView和數據庫
connection = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=database.mdf;Integrated Security=True");
SqlDataAdapter dataAdapter;
DataTable table;
SqlCommand command;
command = new SqlCommand("SELECT * FROM movies", connection);
dataAdapter = new SqlDataAdapter(command);
table = new DataTable();
dataAdapter.Fill(table);
moviesListView.ItemsSource = table.DefaultView;
而這是行不通的。我也嘗試了DataContext而不是ItemsSource,但它沒有幫助。另一方面,當我使用DataGrid時,它工作正常。
請問您能解釋一下原因嗎?
Regards, Vitalii。
什麼你做 意思是_not working_?您收到異常或錯誤訊息? – 2014-12-06 18:02:01
只是看不到內容(ListView爲空)。 – witua 2014-12-06 18:03:52
愚蠢的問題:但是,你是否檢查'table'是否實際上包含任何行? – 2014-12-06 18:06:06