2011-07-21 16 views
10

Uso CheckBoxList en mi aplicación Windows Forms y estoy tratando de aplicarle una fuente de datos.Uso de la fuente de datos con CheckBoxList

Tener un DataTable, 'dt', con columnas id, name y ischecked, utilizo dicho código:

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

¿Cómo se configura CheckState para todos los artículos en MyCheckBoxList?

Guardo este valor en mi datatable y quiero vincularlos con MyCheckBoxList.

+1

¿Podría ser porque 'ValueMember =" id "' en lugar de = "su valor booleano"? – Jay

+0

¿Cómo hiciste esto tanto? Obtengo 'No se puede convertir el tipo 'System.Web.UI.WebControls.CheckBoxList' en 'System.Web.UI.WebControls.ListBox'' si hago esto en una página web. – vapcguy

Respuesta

0

No estoy seguro de que haya una forma de establecer el cheque a través de la propiedad ValueMember.

Se podría dar a este un ir:

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

es una forma, pero en este caso pierdo la ventaja de llamar a la fuente de datos – Grinart

6

he decidido mi problema en dos etapas. En primer lugar, he aplicado la fuente de datos, y luego en el círculo establecer la propiedad activada para cada artículo, como en el siguiente código:

((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

Una CheckedListBox realmente enlazable no requeriría el bucle. De alguna manera, deberíamos vincular la propiedad IsChecked de los elementos de ListBox a una propiedad de los objetos subyacentes. – dotNET

1

Como otra solución alternativa, he implementado el evento CheckedListBox.Format para actualizar automáticamente el CheckedState visualizado con los datos; e implementó el evento CheckedListBox.ItemCheck para actualizar automáticamente los datos con 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

Todo aquí es bueno, excepto el uso del 'BindingSource', ya que no se transfiere a' System.Web'. Me encantaría que aquellos que trabajan en 'System.Windows.Forms' hagan que su código sea lo suficientemente genérico como para ser usado en ambos dominios. Pero me encanta el concepto y definitivamente se pensó mucho en esto, muy funcional para las formas visuales. No es así, para un formulario de página de aplicación web. Si pudieras adaptarte para ambos, me encantaría ver el resultado. – vapcguy

Cuestiones relacionadas