Durgesh Pandey Durgesh Pandey -4 years ago 153
C# Question

How to get CheckedListBox selected item text in Windows Forms

I have a CheckBoxList which contain dynamic data.Now i want to get the list of selected item from CheckBoxList. i am using following code.

for (int i = 0; i < chkTblListDb001.Items.Count; i++)
{
if (chkTblListDb001.GetItemChecked(i))
{
FirstTableSelectedColumns += chkTblListDb001.Items[i].ToString() + ",";
MessageBox.Show(FirstTableSelectedColumns);
}
}


but is return (in FirstTableSelectedColumns variable) "System.Data.DataRowView,System.Data.DataRowView,"

When I am using the this code

string test = "you've selected :";

foreach (ListItem item in theCheckBoxList.Items)
{
test += item.Selected ? item.Value + ", " : "";
}
labelResult.Text = test;


its give me the following error that


ListItem not found.

Answer Source

Solution is ((DataRowView)chkTblListDb001.Items[i])[0].ToString()

 string FirstTableSelectedColumns = "";
      for (int i = 0; i < chkTblListDb001.Items.Count; i++)
            {
              if (chkTblListDb001.GetItemChecked(i))
                 {
                 //FirstTableSelectedColumns += chkTblListDb001.Items[i].ToString() + ",";
                   FirstTableSelectedColumns += ((DataRowView)chkTblListDb001.Items[i])[0].ToString() + ",";
                   MessageBox.Show(FirstTableSelectedColumns);
                 }
             }

Thankx to @Pikoh

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download