How come checkedlistbox does not have datasource ? how to bind to a list of values?

删除回忆录丶 提交于 2019-12-22 01:58:45

问题


I am developing a Winform and I need a checkedlistbox. I have the values stored in an object which has a List property:

public static class Fields
{
    public static IList<string> FieldList { get; set; }

    static Fields()
    { ...//populate FieldList }
}

Now I would like my CheckedListBox to use Fields.FieldList as datasource. After searching online I found I needed to set

//in myForm_Load
mycheckedListBox.DataSource = Fields.FieldList;

But myCheckedListBox does not have a DataSource property.

Am I missing something here?


回答1:


Per the documentation, it should have this property... http://msdn.microsoft.com/en-us/library/system.windows.forms.checkedlistbox.datasource(VS.90).aspx

However, I also had the same issue on a project a while back, and used this CodeProject article to code the solution in the one project where I needed this feature.

Researching a bit more, I did find this:

http://connect.microsoft.com/VisualStudio/feedback/details/115199/checkedlistbox-datasource-displaymember-valuemember-are-hidden

Edit: The above link is no longer working, but the exceprt below is from the article that once resided there.

Posted by Microsoft on 5/30/2005 at 10:28 AM
Thanks for the feedback however this is by design. We do not support data binding on the CheckedListBox control. These properties are inherited from it base class and cannot be removed so we hid them form the property grid and IntelliSense.

That explains why the property exists, but doesn't show in Intellisense.

This blog post is worth a read as well: http://waxtadpole.wordpress.com/2009/10/12/assigning-custom-class-to-checkedlistbox-datasource/




回答2:


Here is how I am binding a List<T> of User objects into CheckedListBox.

((ListBox)myCheckedListBox).DataSource = listOfUsers;
((ListBox)myCheckedListBox).DisplayMember = "FullName";
((ListBox)myCheckedListBox).ValueMember = "UserID";

Of course this is not recommended, since documentation is telling us that this property is hidden.

The code above works, but I noticed some side effects in Visual Studio 2012 such as:

Delay for rendering checked marker:

After you click on the desired item, there is annoying delay to render the "checked" marker.

In my case, CheckOnClick property is True, CausesValidation is False.




回答3:


Personally I use a DataGridView that is bound to a DataTable that has a Boolean field along with a field for the display value.

If you hide the column headers and row headers then you get something pretty close to what a CheckedListBox gives you.




回答4:


This can be worked around by iterating through your would-be datasource and adding its items one-at-a-time. For example:

This, which will cause an exception:

myCheckedListBox.DataSource = myStringList;

Can be modified to this:

foreach (string myString in myStringList)
{
    myCheckedListBox.Items.Add(myString);
}



回答5:


I know this is pretty old; for the benefit of anyone who still has the same requirement, here's what worked for me. Note that I did not use DisplayMember or ValueMember properties, as it seems like it is discouraged (see @David Stratton's post above).

//References
// https://social.msdn.microsoft.com/Forums/vstudio/en-US/0e0da0c9-299e-46d0-b8b0-4ccdda15894c/how-to-assign-values-to-checkedlistbox-items-and-sum-these-values?forum=csharpgeneral


using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace MyApp
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        DatabaseProviderFactory factory = new DatabaseProviderFactory(); 
        Database db = factory.Create("MyConnString");
        DataTable dt = db.ExecuteDataSet(CommandType.StoredProcedure, "ProcGetCustomers").Tables[0];
        var custlist = new List<CheckBoxItem<string, string>>();
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            custlist.Add(Create(dt.Rows[i]["DESC"].ToString(), dt.Rows[i]["CODE"].ToString()));
        }
        checkedListBox1.Items.AddRange(custlist.Cast<object>().ToArray());


   }
    public class CheckBoxItem<K, V>
    {
        public CheckBoxItem(K displayValue, V hiddenValue)
        {
            DisplayValue = displayValue;
            HiddenValue = hiddenValue;
        }

        public K DisplayValue { get; private set; }
        public V HiddenValue { get; private set; }


        public override string ToString()
        {
            return DisplayValue == null ? "" : DisplayValue.ToString();
        }
    }
    public static CheckBoxItem<K, V> Create<K, V>(K displayValue, V hiddenValue)
    {
        return new CheckBoxItem<K, V>(displayValue, hiddenValue);
    }
  }
}



回答6:


I solved the problem by changing the ToString () method to the name that should appear:

public static class Fields
{
   public string MyDisplayMenber{ get; set; }

   public override string ToString(){
         return MyDisplayMenber;
   }
}

and turning into array list with objects:

{
  mycheckedlistbox.Items.AddRange(MyList.ToArray<Fields>());
}


来源:https://stackoverflow.com/questions/8215933/how-come-checkedlistbox-does-not-have-datasource-how-to-bind-to-a-list-of-valu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!