2012-12-29 26 views
2

我是.net的初學者,所以也許我的問題對你們中的一些人來說似乎天真。如何在ChekedListBox中顯示一些記錄

我需要在ChekedListBox中顯示一些記錄。

我有表(CAT)在數據集:

enter image description here

我需要顯示的數據表(色列)的內容在ChekedListBox控制 。

請問任何想法如何實現?

回答

1

看來,CheckedListBox does not support binding,從而預期,這將不起作用:

CheckedListBox1.DataSource = tempDataSet.Tables("Cat") 
CheckedListBox1.DisplayMember = "Color" 
CheckedListBox1.ValueMember = "ID" 

您可以使用Bindable CheckedListBox代替。

enter image description here

+0

Neolisk,將實際工作的檢查情況,如果你是做如下 ((ListBox)CheckedListBox1).DataSource = tempDataSet.Tables(「Cat」); ((ListBox)CheckedListBox1).DisplayMember =「Color」; ((ListBox)CheckedListBox1).ValueMember =「ID;我會親自看看BindingList – MethodMan

+0

@DJKRAZE:DataSource和類似的不是intellisense建議的,但它們都可用,所以編譯器不會阻止你,它甚至會更正的情況下,沒有轉換到ListBox。但是,當通過DataSource綁定時,人們報告了CheckedListBox的問題。[Here is one](http://www.codeproject.com/Articles/4009/Workaround-for-disappearing-checks-在最CheckedL)。 – Neolisk

1

您可以點擊此鏈接:

how to bind data in checkedlistbox in window application

或嘗試這個模板:然後你就可以在設計時使用屬性窗口綁定

SqlDataAdapter da = new SqlDataAdapter("SELECT NAME AC_CODE FROM AccountM where compcode='" + Compcls.Gcomp_cd + "'", con); 

DataSet ds = new DataSet(); 
da.Fill(ds, "AccountM "); 
checkedListBox1.DataSource = ds; 

checkedListBox1.SelectedValue = "AC_CODE"; 
checkedListBox1.SelectedItem = "NAME"; 
0

比方說,如果你用db保存checkedListBox1.SelectedValue,你可以按如下操作:

  1. 將您的數據加載到數據表中,例如, myDt
  2. 循環中的每個數據行的數據表&設置基於dr

    foreach (DataRow dr in myDt.Rows) 
    { 
        checkedListBox1.SelectedValue = dr[0].ToString(); 
        checkedListBox1.SetItemChecked(checkedListBox1.SelectedIndex, true); 
    } 
    

希望這有助於...