【问题标题】:Remove and modify a selected item from listbox that uses datasource从使用数据源的列表框中删除和修改选定项目
【发布时间】:2015-10-29 02:08:31
【问题描述】:

我有一个正在创建的 GUI,它将一个 .csv 文件读入列表框,并且我试图删除一个在应用程序运行时使用按钮选择的国家/地区。我尝试了多个代码,但没有任何效果我收到错误消息“设置 DataSource 属性时无法修改项目集合。”或没有任何反应。以下是我目前所拥有的。我也在尝试使用文本框修改选定的项目。

namespace Countries
{

  public partial class Form1 : Form
  {
      public Form1()
      {
          InitializeComponent();
      }

      private IList<tradingDetails> listOfCountries;

      public class tradingDetails
      {
          public string Country { get; set; }
          public string GDP { get; set; }
          public string Inflation { get; set; }
          public string TB { get; set; }
          public string HDI { get; set; }
          public string TP { get; set; }

          public string Display
          {
              get
              {
                  return string.Format("Country = {0} --- GDP = {1} --- Inflation = {2} --- TB = {3} --- HDI = {4} --- TP = {5}", this.Country, this.GDP, this.Inflation, this.TB, this.HDI, this.TP);

              }
          }
      }

      public static string[] headers { get; set; }

      public void load_Click(object sender, EventArgs e)
      {
          this.listOfCountries = new List<tradingDetails>();
          this.listBox1.ValueMember = "Countries";
          this.listBox1.DisplayMember = "Display";
          this.InsertInfo();
          this.listBox1.DataSource = this.listOfCountries;
      }

      public void InsertInfo()
      {
          OpenFileDialog browse = new OpenFileDialog();
          browse.Multiselect = true;
          if (browse.ShowDialog() == DialogResult.OK)
          {
              string selectedFile = browse.FileName;
              const int MAX_SIZE = 5000;
              string[] AllLines = new string[MAX_SIZE];

              AllLines = File.ReadAllLines(selectedFile);
              foreach (string line in AllLines)
              {
                  if (line.StartsWith("Country"))
                  {
                    headers = line.Split(',');
                  }
                  else
                  {
                      string[] columns = line.Split(',');

                      tradingDetails fileCountry = new tradingDetails
                      {
                          Country = columns[0],
                          GDP = columns[1],
                          Inflation = columns[2],
                          TB = columns[3],
                          HDI = columns[4],
                          TP = columns[5]
                      };

                      this.listOfCountries.Add(fileCountry);

                  }
              }
          }
      }

      private void DataBind()
      {
          listBox1.BeginUpdate();
          listBox1.DataSource = listOfCountries;
          listBox1.EndUpdate();
      }
      private void remove_Click(object sender, EventArgs e)
      {
         for (int x = listBox1.SelectedIndices.Count - 1; x >= 0; x--)
          {
              int idx = listBox1.SelectedIndices[x];
              listBox1.Items.RemoveAt(idx);
          }
      }

      private void search_Click(object sender, EventArgs e)
      {
          listBox1.SelectedItems.Clear();

          for (int i = 0; i < listBox1.Items.Count; i++)
          {
              if (listBox1.Items[i].ToString().Contains(textBox1.Text))
              {
                  listBox1.SetSelected(i, true);
              }
          }
      }

      private void button2_Click(object sender, EventArgs e)
      {
          textBox2.Text = listBox1.Items.Count.ToString();
      }
  }

}

更新 我已经尝试过了,但是这会删除组合框中的所有信息,而不是单个项目。

    private void remove_Click(object sender, EventArgs e)
    {
        comboBox1.DataSource = null;
        comboBox1.Items.Remove(comboBox1.SelectedValue);
        comboBox1.DataSource = listOfCountries;

    }

【问题讨论】:

    标签: c# winforms listbox datasource


    【解决方案1】:

    当列表框与源绑定时,您无法从列表框中删除项目。为了更好地理解,您正在尝试删除该项目,您的列表框不是其所有者,但来源是(您已设置列表框的数据源)。 相反,您需要从数据源本身中删除该项目。

    private void remove_Click(object sender, EventArgs e)
    {
        for (int x = listBox1.SelectedIndices.Count - 1; x >= 0; x--)
        {
            int idx = listBox1.SelectedIndices[x];
            //listBox1.Items.RemoveAt(idx);
            listOfCountries.RemoveAt(idx)l
        }
        listBox1.RefreshItems();
    }
    

    此外,当您尝试清除列表框中的所有项目时,这不是迭代每个项目并删除所有项目的好方法。相反,您应该清除 listOfCountries 或将 listbox1datasource 设置为 null。

    【讨论】:

    • Iv 在它不起作用之前尝试过,我不想实际删除数据本身,只需将其从列表中删除即可。 RefreshItem 也不起作用。我也在考虑将框更改为组合框,但我不确定这是否需要重写整个代码。
    • 在这种情况下,将列表框与删除特定项目的临时源绑定。
    • 我不知道该怎么做......对不起,我对这一切有点陌生
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-18
    • 1970-01-01
    相关资源
    最近更新 更多