Problem With CheckedListBox

I' have more then one cheked objects in the list but I get the same (first) value all the time, despite the index is change.

Hear Is the Code

strQuery = "SELECT code,name FROM tbl001 ORDER BY name";

orclDa = new OracleDataAdapter(strQuery, strOrclConn);

orclDa.Fill(orclDs, "tbl001");

clbMifalim.DataSource = orclDs.Tables["tbl001"];

clbMifalim.DisplayMember = "name";

clbMifalim.ValueMember = "code";



foreach (int i in clbMifalim.CheckedIndices)

{

MessageBox.Show("Index#: " + i.ToString() + ", Value is:" +

clbMifalim.SelectedValue + ".");

}

Thanks



Answer this question

Problem With CheckedListBox

  • M D Larsen

    Try this code....

                DataSet ds = new DataSet("HiHi");
                ds.Tables.Add("Kings");
                ds.Tables[0].Columns.Add("Name", System.Type.GetType("System.String"));
                ds.Tables[0].Columns.Add("Id", System.Type.GetType("System.Int32"));
                DataRow dr;
                dr = ds.Tables[0].NewRow();
                dr[0] = "AAA";
                dr[1] = 1;
                ds.Tables[0].Rows.Add(dr);

                dr = ds.Tables[0].NewRow();
                dr[0] = "BBB";
                dr[1] = 2;
                ds.Tables[0].Rows.Add(dr);

                dr = ds.Tables[0].NewRow();
                dr[0] = "CCC";
                dr[1] = 3;
                ds.Tables[0].Rows.Add(dr);
                this.checkedListBox1.DataSource = ds.Tables[0];
                this.checkedListBox1.DisplayMember = "Name";
                this.checkedListBox1.ValueMember = "Id";

              // following code is to read the selected items.

                foreach(object obj in this.checkedListBox1.CheckedItems)
                {
                    MessageBox.Show(((DataRowView) obj)[0].ToString() + ",  " +  ((DataRowView) obj)[1].ToString());
                }

    Regards,
    Siva.M


  • vnpro

    Siva, Very nice example but this doesn't work. It gives you an ID of last item selected. Means if you have more then 1 items is checked. This example gets only the first item was selected within the same checkedlistbox. I have been searching on net for last 2 days and haven't found any solid guide as how I can get the CheckListBox1.SelectedValue for more then 1 items is checked. Please share with me your experties on this. Thanks a lot.


  • el_d

    CheckedListBox.SelectedValue and .SelectedIndex contain the values for the last checked entry only.  You can access the selected items through the CheckedItems and get the value for each item, or use CheckedIndices to index into the underlying dataset.


  • JanDV

    Can you send me code example for :
    selected items through the CheckedItems and get the value for each item

    Regards,
    boaz.

  • Problem With CheckedListBox