2012-08-01 52 views
0
private void table_loader() 
{ 
    SqlCeConnection ucon = new SqlCeConnection(); 
    ucon.ConnectionString = " Data Source=C:\\Documents and Settings\\laser\\My Documents\\Visual Studio 2008\\Projects\\dbtest\\dbtest\\ulist.sdf"; 
    ucon.Open(); 
    SqlCeCommand uce = new SqlCeCommand(); 
    SqlCeDataAdapter uced = new SqlCeDataAdapter(); 
    uced.SelectCommand = new SqlCeCommand("SELECT * FROM plist", ucon); 
    uced.Fill(ulistDataSet, "plist"); 
    DataTable dplist = ulistDataSet.Tables["plist"]; 
    for (int i = 0; i <dplist.Rows.Count;i++) 
    { 
     DataRow drow = dplist.Rows[i]; 

      ListViewItem lvi = new ListViewItem(drow["Process"].ToString()); 
      MessageBox.Show(lvi.Text); 
      listView2.Items.Add(lvi); 

    } 

} 

我用上面的代碼來顯示數據庫(SQL Server CE 3.5)的列表視圖值,但列表視圖不顯示任何東西!當循環執行時,消息框()也會逐個正確顯示數據庫值,我認爲問題必須是爲listview添加值。這個代碼的錯誤,任何人都可以解釋嗎?對不起我可憐的英語!增加值的ListView

在此先感謝, Ulaga

回答

1

嘗試使用此代碼

listView2.Items.Add(drow["Process"].ToString()); //Verify that column's name is Process 

答案用foreach語法

foreach(var row in dplist.Rows) 
{ 
    listView2.Items.Add(row["Process"].ToString()); 
}