1

I've got this code to try to extract the display value from a CheckedListBox:

CheckedListBox.CheckedItemCollection selectedUnits = checkedListBoxUnits.CheckedItems;
_selectedUnit = selectedUnits[0].ToString();

...but it doesn't work - the value of "_selectedUnit", instead of being "platypus" as it should be, is "System.Data.DataRowView".

How can I coax the string value out of this complex object?

UPDATE

I'm not sure just what user2946329 wants to see bzg. my CheckedListBox, but here is how it is populated:

private void PopulateUnits()
{
    using (SqlConnection con = new SqlConnection(ReportRunnerConstsAndUtils.CPSConnStr))
    {
        using (SqlCommand cmd = new SqlCommand(ReportRunnerConstsAndUtils.SelectUnitsQuery, con))
        {
            cmd.CommandType = CommandType.Text;
            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
            {
                DataTable dt = new DataTable();
                sda.Fill(dt);
                ((ListBox)checkedListBoxUnits).DataSource = dt;
                ((ListBox)checkedListBoxUnits).DisplayMember = "Unit";
                ((ListBox)checkedListBoxUnits).ValueMember = "Unit";
            }
        }
    }
}

Let me know if there is anything missing that would help you.

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

4 Answers4

2

It should be something like this:

DataRowView dr = checkedListBoxUnits.CheckedItems[0] as DataRowView;
string Name = dr["Unit"].ToString();
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
1

Judging by the string you get ("System.Data.DataRowView"), you use the CheckListBox with datasource attached. If that's the case, in CheckedItems you really get DataRowViews.. hence the string, since its ToString() returns class name. You will need to access the data from the DataRowView, by column name or index.

Jiri P.
  • 421
  • 3
  • 10
1
_selectedUnit = ((DataRowView)selectedUnits[0])["Name"].ToString();

the key is to typecast the item before accessing it...

MaxOvrdrv
  • 1,780
  • 17
  • 32
0

Combining user2946329's answer with Resharper's contribution, the code I ended up using is:

String _selectedUnit;
. . .
DataRowView dr = checkedListBoxUnits.CheckedItems[0] as DataRowView;
if (dr != null) _selectedUnit = dr["Unit"].ToString();

(Resharper complained about a possible null reference issue, and so added the "if (dr != null)" jazz to the last line.

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 1
    You could also get OutOfRange exception if there are none items checked in the list, since you directly access item at index 0 without any check whether or not there is such item. – Jiri P. Dec 22 '15 at 23:41
  • Yes, theoretically it's not possible, because the "Go" button is disabled until a unit has been selected (among other things), but I know it's better to be defensive than offensive when it comes to programming. – B. Clay Shannon-B. Crow Raven Dec 22 '15 at 23:45