简体   繁体   English

设置DataSource属性时,无法修改c#项目集合

[英]c# Items collection cannot be modified when the DataSource property is set

I'm trying to move item up/down a listbox but keep getting an error, not sure how to fix it. 我正在尝试在列表框中上移/下移项目,但始终收到错误消息,不确定如何解决。 the error has something to with " listBox1.Items.Remove(selected); " 错误与“ listBox1.Items.Remove(selected); ”有关

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace company1
{
    public partial class Form1 : Form
    {
        List<Configuration> lines = new List<Configuration>();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.listBox1.Items.Clear();
            //Read in every line in the file
            using (StreamReader reader = new StreamReader("file.txt"))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    //string textfile = line;
                    string[] myarray = new string[] { "\\n" };
                    string[] parts = new string[9];
                    parts = line.Split(myarray, StringSplitOptions.RemoveEmptyEntries);
                    lines.Add(new Configuration(parts[0], int.Parse(parts[1]), int.Parse(parts[2])));
                    line = reader.ReadLine();
                }

            }
            listBox1.DataSource = lines;
            listBox1.DisplayMember = "CompanyName";
        }

        private void moveup_button_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedItems.Count > 0)
            {
                object selected = listBox1.SelectedItem;
                int indx = listBox1.Items.IndexOf(selected);
                int totl = listBox1.Items.Count;

                if (indx == 0)
                {
                    listBox1.Items.Remove(selected);
                    listBox1.Items.Insert(totl - 1, selected);
                    listBox1.SetSelected(totl - 1, true);
                }
                else
                {
                    listBox1.Items.Remove(selected);
                    listBox1.Items.Insert(indx - 1, selected);
                    listBox1.SetSelected(indx - 1, true);
                }
            }
        }

        private void movedown_button_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedItems.Count > 0)
            {
                object selected = listBox1.SelectedItem;
                int indx = listBox1.Items.IndexOf(selected);
                int totl = listBox1.Items.Count;

                if (indx == totl - 1)
                {
                    listBox1.Items.Remove(selected);
                    listBox1.Items.Insert(0, selected);
                    listBox1.SetSelected(0, true);
                }
                else
                {
                    listBox1.Items.Remove(selected);
                    listBox1.Items.Insert(indx + 1, selected);
                    listBox1.SetSelected(indx + 1, true);
                }
            }
        }
    }
}

Use a BindingList object: 使用BindingList对象:

  • Create a BindingList using your initial items as data 使用您的初始项目作为数据创建BindingList
  • Bind your listbox's data source to that BindingList object 将列表框的数据源绑定到该BindingList对象
  • Add and remove items from the BindingList, and not from the listbox 从BindingList而不是从列表框中添加和删除项目

Good example at http://msdn.microsoft.com/en-us/library/ms132679.aspx http://msdn.microsoft.com/zh-cn/library/ms132679.aspx上的好例子

 Conn obcon = new Conn();
 SqlConnection ob = new SqlConnection(obcon.strCon);
 SqlDataAdapter da = new SqlDataAdapter();
 da.SelectCommand = new SqlCommand();
 da.SelectCommand.Connection = ob;
 SqlCommand ds = da.SelectCommand;
 ds.CommandText = "Select* from UserManagement";
 ds.CommandType = CommandType.Text;
 DataTable dt = new DataTable();
 da.Fill(dt);
 DataTable dtt = new DataTable();
 dtt.Columns.Add("FullName");
 dtt.Rows.Add();
 dtt.Rows[0]["FullName"] = "Select Name";
 for (int i = 0; i < dt.Rows.Count; i++)
 {
     dtt.Rows.Add();
     dtt.Rows[i + 1]["FullName"] = dt.Rows[i][0].ToString();
 }
 cmbFindUser.DataSource = dtt;
 cmbFindUser.DisplayMember = "FullName";
 cmbFindUser.ValueMember = "FullName";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在c#中设置DataSource属性时,无法修改项目集合 - Items collection cannot be modified when the DataSource property is set in c# 设置DataSource属性后,将无法修改项目集合。 C# - Items collection cannot be modified when the DataSource property is set. c# System.ArgumentException:“设置 DataSource 属性时无法修改项目集合。” C# Windows Forms - System.ArgumentException: 'Items collection cannot be modified when the DataSource property is set.' C# Windows Forms C#ListBox:设置DataSource属性时,无法修改Items集合 - C# ListBox : Items collection cannot be modified when the DataSource property is set 在C#Windows中设置DataSource属性时,无法修改项目集合 - Items collection cannot be modified when the DataSource property is set in C# Windows 列表框错误:设置了DataSource属性时,无法修改项目集合 - listbox error: Items collection cannot be modified when the DataSource property is set 例外:设置DataSource属性时,无法修改项集合 - Exception : Items collection cannot be modified when the DataSource property is set 设置DataSource属性时,无法修改项目集合 - Items collection cannot be modified when the DataSource property is set 错误:其他信息:设置DataSource属性时,不能修改项目集合 - ERROR: Additional information: Items collection cannot be modified when the DataSource property is set System.ArgumentException:设置DataSource属性时,不能修改Items集合 - System.ArgumentException: Items collection cannot be modified when the DataSource property is set
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM