2011-07-21 15 views
10

Tôi sử dụng CheckBoxList trong ứng dụng Windows Forms của mình và đang cố gắng áp dụng nguồn dữ liệu cho nó.Sử dụng nguồn dữ liệu với CheckBoxList

Có một DataTable, 'dt', với các cột id, nameischecked, tôi sử dụng mã ví dụ:

((ListBox)MyCheckBoxList).DataSource = dt; 
((ListBox)MyCheckBoxList).DisplayMember = "name"; 
((ListBox)MyCheckBoxList).ValueMember = "id"; 

Làm thế nào để tôi thiết CheckState cho tất cả các mục trong MyCheckBoxList?

Tôi giữ giá trị này trong mục dữ liệu của mình và muốn liên kết chúng với MyCheckBoxList.

+1

Có thể vì 'ValueMember =" id "' thay vì = "giá trị boolean của bạn"? – Jay

+0

Làm thế nào bạn thậm chí làm được điều này nhiều? Tôi nhận được 'Không thể chuyển đổi loại 'System.Web.UI.WebControls.CheckBoxList' thành 'System.Web.UI.WebControls.ListBox'' nếu tôi làm điều này trên một trang web. – vapcguy

Trả lời

0

Tôi không chắc chắn có cách nào để đặt kiểm tra thông qua thuộc tính ValueMember.

Bạn có thể cung cấp cho này một đi:

foreach (DataRow item in dt.Rows) 
{ 
    MyCheckedListBox.Items.Add(item["Name"].ToString(), Convert.ToBoolean(item["Checked"])); 
} 
+1

đó là một cách nhưng trong trường hợp này tôi mất lợi thế để gọi cho nguồn dữ liệu – Grinart

6

tôi đã quyết vấn đề của tôi trong hai bước. Trước tiên, tôi áp dụng các nguồn dữ liệu, và sau đó trong vòng tròn thiết lập thuộc tính Checked cho từng hạng mục như trong đoạn mã sau:

((ListBox)MyCheckBoxList).DataSource = dt; 
((ListBox)MyCheckBoxList).DisplayMember = "name"; 
        ... 

for (int i = 0; i < folloving.Items.Count; i++) 
{ 
    DataRowView row = (DataRowView)MyCheckBoxList.Items[i]; 
    bool isChecked = Convert.ToBoolean(row["checked"]); 
    MyCheckBoxList.SetItemChecked(i, isChecked); 
} 
+2

Một CheckedListBox thực sự có thể ràng buộc sẽ không yêu cầu vòng lặp. Chúng ta bằng cách nào đó có thể ràng buộc tài sản IsChecked của các mục ListBox với một thuộc tính của các đối tượng bên dưới. – dotNET

1

Như công việc xung quanh khác, tôi thực hiện sự kiện CheckedListBox.Format để tự động cập nhật các CheckedState hiển thị với dữ liệu; và triển khai sự kiện CheckedListBox.ItemCheck để tự động cập nhật dữ liệu với CheckedState.

private DataTable myDataTable = null; 
private BindingSource myBindingSource = new BindingSource(); 

// Column Name Constants 
private const string C_ITEM_INDEX = "Item_Index";  // CheckedListBox Item's Index 
private const string C_ITEM_TEXT = "Item_Text";  // Item's Text 
private const string C_ITEM_CHECKED = "Item_Checked"; // Item's Checked State 
private const string C_DATA_KEY = "Data_Key";   // Arbitrary Key Value for Relating Item to Other Data 

private void Startup() 
{ 
    // Create DataTable 
    // This DataTable has 4 Columns described by the constants above 
    myDataTable = new DataTable(); 
    myDataTable.Columns.Add(new DataColumn(C_ITEM_INDEX, typeof(Int32))); 
    myDataTable.Columns[C_ITEM_INDEX].DefaultValue = 0; 
    myDataTable.Columns.Add(new DataColumn(C_ITEM_TEXT, typeof(string))); 
    myDataTable.Columns[C_ITEM_TEXT].DefaultValue = ""; 

    // I personally like Integer 1=true, 0=false values. typeof(bool) will also work. 
    myDataTable.Columns.Add(new DataColumn(C_ITEM_CHECKED, typeof(Int32))); 
    myDataTable.Columns[C_ITEM_CHECKED].DefaultValue = 0; 

    // Other columns can be included in the DataTable 
    myDataTable.Columns.Add(new DataColumn(C_DATA_KEY, typeof(Int32))); 
    myDataTable.Columns[C_DATA_KEY].DefaultValue = 0;  
    myDataTable.AcceptChanges(); 

    // Bind the DataTable's DefaultView to the CheckedListBox 
    myBindingSource.DataSource = myDataTable.DefaultView; 
    this.myCheckedListBox.DataSource = myBindingSource; 

    // Set the DisplayMember to the DataColumn you want displayed as the CheckedListBox Items's Text 
    this.myCheckedListBox.DisplayMember = C_ITEM_TEXT; 

    // Set the ValueMember to the Data. Note: The ValueMember is not displayed and is Not the CheckBox value. 
    this.myCheckedListBox.ValueMember = C_DATA_KEY; 

    // Hookup Event Handler for the CheckedListBox.Format Event. 
    /// * The Format event enables us to just in time update the CheckBoxes with the values in the DataTable 
    this.myCheckedListBox.Format += myCheckedListBox_Format; 

    // Hookup Event Handler for the CheckedListBox.ItemCheck Event. 
    /// * The ItemCheck event enables us to just in time update the DataTable with the values from the Item CheckBoxes 
    this.myCheckedListBox.ItemCheck += myCheckedListBox_ItemCheck; 
} 


void myCheckedListBox_Format(object sender, ListControlConvertEventArgs e) 
{ 
    /// * The Format event enables us to just in time update the CheckBoxes with the values in the DataTable 
    // Retrieve the Index of the Item in the CheckedListBox by finding the DataRowView in the BindingSource 
    // Note: Use a column with unique values for the BindingSource.Find() function 
    int listindex = myBindingSource.Find(C_ITEM_INDEX, ((DataRowView)e.ListItem)[C_ITEM_INDEX]); 

    // The argument, e.ListItem, is the current DataRowView in the DataTable's DefaultView 
    // Check to see if the checkbox value is different from the data 
    if (((CheckedListBox)sender).GetItemChecked(listindex) != Convert.ToBoolean(((DataRowView)e.ListItem)[C_ITEM_CHECKED])) 
    { 
    // Set the CheckList Item's CheckState to match the data 
    ((CheckedListBox)sender).SetItemChecked(listindex, Convert.ToBoolean(((DataRowView)e.ListItem)[C_ITEM_CHECKED])); 
    } 
} 


void myCheckedListBox_ItemCheck(object sender, ItemCheckEventArgs e) 
{ 
    /// * The ItemCheck event enables us to just in time update the DataTable with the values from the Item CheckBoxes 
    // Update the data with the new CheckState value. 
    if (e.NewValue == CheckState.Checked) 
    { 
    myDataTable.DefaultView[e.Index][C_ITEM_CHECKED] = 1; 
    } 
    else 
    { 
    myDataTable.DefaultView[e.Index][C_ITEM_CHECKED] = 0; 
    } 
    // Update other data values too if you need to. 
} 
+0

Mọi thứ ở đây đều tốt ngoại trừ việc sử dụng 'BindingSource', vì nó không chuyển sang' System.Web'. Rất thích những người làm việc trong 'System.Windows.Forms' để làm cho mã của họ đủ chung để được sử dụng trong cả hai cõi. Nhưng tôi yêu khái niệm và chắc chắn có rất nhiều suy nghĩ đi vào điều này - rất chức năng cho các hình thức trực quan. Không phải như vậy, cho một mẫu trang ứng dụng web. Nếu bạn có thể thích nghi cho cả hai, tôi rất muốn xem kết quả. – vapcguy