【问题标题】:Select multiple items in ListBox programmatically?以编程方式选择 ListBox 中的多个项目?
【发布时间】:2019-10-06 00:29:10
【问题描述】:

我正在尝试通过 C# 选择 ListBox 项目。我无法选择多个项目,它总是选择我所选项目列表中的最后一个值。

我尝试遍历每个ListBoxItem 并检查它是否存在于我的选定项目列表中。

 List<int> SelectedItems = new List<int>();
 SqlCommand cmd = new SqlCommand(Query, con)
 con.Open();
 SqlDataReader dr = cmd.ExecuteReader();
 while (dr.Read()) {
   SelectedItems.Add(Convert.ToInt32(dr["RecordId"].ToString()));
 }
 for (int i = 0; i < listbox.Items.Count; i++) {
     int x = Convert.ToInt32(listbox.Items[i].Value.ToString());
     if(SelectedItems.Contains(x)){
     listbox.SelectionMode = listbox.Multiple;
     listbox.SelectedIndex = i;
     }
  }

我需要选择多个项目,而不仅仅是最后选择的值。我做错了什么?

【问题讨论】:

    标签: c# asp.net listbox multi-select


    【解决方案1】:

    不要设置SelectedIndex 属性,而是尝试使用listbox.SetSelectedlistbox.SetSelected(x,true); 添加项目

    【讨论】:

    • SetSelected 不受支持。
    • @ArmaanLabib “不支持”是什么意思?
    【解决方案2】:

    你可以用这个:

    LblDisplay.Text = "";
    lstPromote_products.SetItemChecked(4, true);
    chkRestricted.Checked = true;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-17
      • 1970-01-01
      • 1970-01-01
      • 2014-03-02
      • 2017-12-11
      • 2012-07-17
      • 1970-01-01
      相关资源
      最近更新 更多