如何在CheckedListBox中检索CheckedItems的文本

时间:2016-06-10 12:15:18

标签: c# .net winforms data-binding checkedlistbox

我有一个包含动态数据的CheckBoxList。现在我想从CheckBoxList获取所选项目的列表。我正在使用以下代码。

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

但是返回(在FirstTableSelectedColumns变量中)“System.Data.DataRowView,System.Data.DataRowView,”

当我使用此代码时

string test = "you've selected :";

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

它给我以下错误

  

未找到ListItem。

4 个答案:

答案 0 :(得分:4)

要在CheckedListBox中获取项目的文字,您可以使用GetItemText

项目的类型无关紧要,如果您使用DataSourceDisplayMember使用DisplayMember来返回文字,则使用ToString方法项目。

以下是您要查找的内容的示例,以逗号分隔的已检查项目文本列表:

var texts = this.checkedListBox1.CheckedItems.Cast<object>()
                .Select(x => this.checkedListBox1.GetItemText(x));

MessageBox.Show(string.Join(",", texts));

答案 1 :(得分:1)

  

解决方案是((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);
                 }
             }

感谢@Pikoh

答案 2 :(得分:0)

使用 CheckedItems 方法。

dta <- structure(list(ID = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 4L, 4L, 4L), 
                      CLM_ID = c(718182L, 718184L, 885236L, 885362L, 589963L, 589999L, 594226L, 689959L, 656696L, 669625L, 777777L, 778952L, 778965L), 
                      Date1 = structure(c(1L, 3L, 2L, 9L, 8L, 6L, 10L, 12L, 11L, 13L, 5L, 4L, 7L), .Label = c("1/1/2014", "1/15/2014", "1/2/2014", "2/1/2015", "2/21/2015", "2/27/2015", "3/1/2015", "3/18/2015", "3/20/2014", "4/11/2015", "5/1/2016", "5/10/2015", "5/6/2016"), class = "factor"), 
                      Date2 = structure(c(1L, 2L, 1L, 4L, 5L, 10L, 7L, 11L, 9L, 8L, 6L, 3L, 5L), .Label = c("1/17/2014", "1/8/2014", "2/28/2015", "3/21/2014", "3/22/2015", "3/4/2015", "4/17/2015", "5/22/2016", "5/5/2016", "5/9/2015", "6/10/2015"), class = "factor")), 
                 .Names = c("ID", "CLM_ID", "Date1", "Date2"), class = "data.frame", 
                 row.names = c(NA, -13L))

答案 3 :(得分:0)

尝试以下操作,将此类添加到项目中

using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
public static class CheckedListBoxExtensions
{
    public static string CheckedItemsJoined(this CheckedListBox sender)
    {
        var Result = from singleItem in sender.Items.OfType<string>().Where(
                         (item, index) =>
                         {
                             return sender.GetItemChecked(index);
                         }
                     )
                     select singleItem;

        return string.Join(",", Result.ToList<string>());
    }
}

现在在表单中,使用在此之后建模的代码(意味着您需要更改控件的名称以匹配您的控件。

someLabel.Text = checkedListBox1.CheckedItemsJoined();

希望这就是你所追求的目标。