简体   繁体   中英

How to get value member of checked items in checkedlistbox windows control?

am using the following code to bind a checkedlistbox in win form.I want to get the value member of of checked items in checkedlistbox?

listCollection = new List<ListItem>();
            listCollection.Add(new ListItem { text = "Manufacturer", value = "1" });
            listCollection.Add(new ListItem { text = "Dealer", value = "2" });
            listCollection.Add(new ListItem { text = "Distributor", value = "3" });
            listCollection.Add(new ListItem { text = "Trader", value = "4" });
            listCollection.Add(new ListItem { text = "Service Provider", value = "5" });
            chkListCategory.DataSource = listCollection;
            chkListCategory.DisplayMember = "text";
            chkListCategory.ValueMember = "value";

I don't know what is ListItem but I suppose it is a class that is looks like:

public class ListItem
{
    public string Text;
    public object Value;

    public ListItem(string text, object value)
    { /*...*/ }
}

So, change the DisplayMember = "text"; to "Text" and ValueMember = "value"; to "Value" :

chkListCategory.DisplayMember = "Text";//"text"; 
chkListCategory.ValueMember = "Value";//"value";

The display text at UI will be "Manufacturer, Dealer, Distributor, ..."

And the values will be "1, 2, 3, ..."

Get the value member of checked items:

To get the values of checked items:

//first checked item.
var value = (chkListCategory.CheckedItems[0] as ListItem).Value;

//all checked items.
foreach (var value in chkListCategory.CheckedItems)
{
    Console.WriteLine((value as ListItem).Value);
}

//value at any index in the chkListCategory:
var value = (chkListCategory.Item[index] as ListItem).Value;

Bind CheckedListBOx or any other control with displaymember and valuemember is quite simple you just need to specify datasource property of control as well as displaymember and valuemember.

Following is working code 100 % work for me i have tested:

/* checkedlistbox bindig code */

DataSet ds = new DataSet();

string strChechboxlist = "select Subject_ID as code, SubjectName as Display from dbo.Mst_Subject_Detail";

/* filldataset() is function i have created to return dataset. */
ds = dc.FillDataSet(strChechboxlist);
if (ds.Tables[0].Rows.Count > 0)
{
checkedListBox1.DataSource = ds.Tables[0];
checkedListBox1.DisplayMember = "Display";
checkedListBox1.ValueMember = "code"; 

}

/* for fetching valuemember or displaymember from checkedlistbox */


for(int i = 0; i < checkedListBox1.CheckedItems.Count; i++)
{

/*Now with the following code we can get valemember and displaymember as per your requirement you can store in table. */
DataRow r;
r = ((DataRowView)this.checkedListBox1.CheckedItems[i]).Row;
string val = (r[this.checkedListBox1.ValueMember]).ToString();
string dis = (r[this.checkedListBox1.DisplayMember]).ToString();
r = null;

}

Note:- I am attaching working demo of code

Here is the code to get some records from Database and put these in a CheckedListBox

SqlCommand cmd = new SqlCommand(@"SELECT Code, GenericName FROM tbl_Item", Connection);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        chklItems.DataSource = null; // chklItems is a CheckedListBox
        if (chklItems.Items.Count > 0)
            chklItems.Items.Clear();
        chklItems.DataSource = dt;
        chklItems.DisplayMember = "GenericName";
        chklItems.ValueMember = "Code";


To Get value member of checked items

for (int i = 0; i < chklItems.CheckedItems.Count; i++)
        {
            string code = ((DataRowView)chklItems.CheckedItems[i]).Row["Code"].ToString();
        }
foreach(DataRowView view in chkListCategory.CheckedItems)
{
    Console.WriteLine(view[chkListCategory.ValueMember].ToString());
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM