从使用数据源的列表框中删除和修改所选项

时间:2015-08-06 04:50:54

标签: c# winforms listbox datasource

我正在创建一个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;

    }

1 个答案:

答案 0 :(得分: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或将listbox1数据源设置为null。