如何从CheckedListBox获取选中项的字符串值? [英] How can I get the string value of a checked item from a CheckedListBox?

查看:100
本文介绍了如何从CheckedListBox获取选中项的字符串值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码尝试从CheckedListBox中提取显示值:

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

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

...但是它不起作用-"_selectedUnit"的值不是" platypus ",而是" System.Data.DataRowView ".

...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?

我不确定user2946329想要看到什么bzg.我的CheckedListBox,但这是它的填充方式:

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.

推荐答案

应该是这样的:

DataRowView dr = checkedListBoxUnits.CheckedItems[0] as DataRowView;
string Name = dr["Unit"].ToString();

这篇关于如何从CheckedListBox获取选中项的字符串值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆