【问题标题】:OleDB Exception when trying to read directories尝试读取目录时出现 OleDB 异常
【发布时间】:2015-08-02 18:52:23
【问题描述】:

我无法解决我的问题,但我在网络上没有找到任何符合我情况的解决方案。我有一个 CheckBoxList,只是想获取已签入列表的项目。

代码:

  foreach (ListItem item in checkedMonthsBox.Items)
        {
            if (item.Selected)
            {
                months.Add(item);
            }
        }

我得到一个 System.InvalidCastException 和

““System.String”的对象不能转换成 "System.Web.UI.WebControls.ListItem"。

月份列表来自类型“ListItem”。

如何解决?

【问题讨论】:

    标签: c# winforms casting checkboxlist listitem


    【解决方案1】:

    好的,我解决了!

    for (int i = 0; i < checkedMonthsBox.Items.Count; i++)
            {
                if (checkedMonthsBox.GetItemCheckState(i) == CheckState.Checked)
                {
                    months.Add(checkedMonthsBox.Items[i].ToString());
                }
             }
    

    【讨论】:

    • 您使用 months.Add(item.ToString()); 的旧代码可能也可以使用。
    • 他在第一行开始循环时崩溃了。
    猜你喜欢
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多