设置DataSource属性时,无法修改c#Items集合
我试图在列表框中上下移动项目,但不断出现错误,不确定如何修复。该错误与“设置DataSource属性时,无法修改c#Items集合,c#,.net,visual-studio-2010,C#,.net,Visual Studio 2010,我试图在列表框中上下移动项目,但不断出现错误,不确定如何修复。该错误与“listBox1.Items.Remove(selected);”有关 使用系统; 使用System.Collections.Generic; 使用系统组件模型; 使用系统数据; 使用系统图; 使用System.Linq; 使用系统文本; 使用System.Windows.Forms; 使用System.IO; 名称空间公司1 { 公共部分类Form1:Form { 列表行=新列表(); 公共表格1() { 初始化组件();
listBox1.Items.Remove(selected);
”有关
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用System.IO;
名称空间公司1
{
公共部分类Form1:Form
{
列表行=新列表();
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
this.listBox1.Items.Clear();
//读入文件中的每一行
使用(StreamReader=newstreamreader(“file.txt”))
{
弦线;
而((line=reader.ReadLine())!=null)
{
//字符串textfile=line;
字符串[]myarray=新字符串[]{“\\n”};
字符串[]部分=新字符串[9];
parts=line.Split(myarray、StringSplitOptions.RemoveEmptyEntries);
Add(新配置(部分[0]、int.Parse(部分[1])、int.Parse(部分[2]));
line=reader.ReadLine();
}
}
listBox1.DataSource=行;
listBox1.DisplayMember=“CompanyName”;
}
私有无效上移按钮单击(对象发送者,事件参数e)
{
如果(listBox1.SelectedItems.Count>0)
{
所选对象=listBox1.SelectedItem;
int indx=listBox1.Items.IndexOf(选中);
int totl=listBox1.Items.Count;
如果(indx==0)
{
listBox1.Items.Remove(选中);
listBox1.Items.Insert(totl-1,选中);
listBox1.SetSelected(totl-1,true);
}
其他的
{
listBox1.Items.Remove(选中);
listBox1.Items.Insert(indx-1,选中);
listBox1.SetSelected(indx-1,true);
}
}
}
私有无效向下移动按钮单击(对象发送者,事件参数e)
{
如果(listBox1.SelectedItems.Count>0)
{
所选对象=listBox1.SelectedItem;
int indx=listBox1.Items.IndexOf(选中);
int totl=listBox1.Items.Count;
如果(indx==totl-1)
{
listBox1.Items.Remove(选中);
listBox1.Items.Insert(0,选中);
listBox1.SetSelected(0,true);
}
其他的
{
listBox1.Items.Remove(选中);
listBox1.Items.Insert(indx+1,选中);
listBox1.SetSelected(indx+1,true);
}
}
}
}
}
使用BindingList对象:
- 使用初始项作为数据创建绑定列表
- 将listbox的数据源绑定到该BindingList对象
- 从BindingList(而不是listbox)中添加和删除项
Conn obcon=new Conn()上有一个很好的例子;
SqlConnection ob=新的SqlConnection(obcon.strCon);
SqlDataAdapter da=新的SqlDataAdapter();
da.SelectCommand=新建SqlCommand();
da.SelectCommand.Connection=ob;
SqlCommand ds=da.SelectCommand;
ds.CommandText=“从用户管理中选择*”;
ds.CommandType=CommandType.Text;
DataTable dt=新的DataTable();
da.填充(dt);
DataTable dtt=新的DataTable();
dtt.列。添加(“全名”);
dtt.Rows.Add();
dtt.行[0][“全名”]=“选择名称”;
对于(int i=0;i
您可以直接将项目添加到列表框中,而无需设置数据源。然后你就可以直接操纵它们了。这个问题讨论了如何:或者您可以通过从行中删除行,然后进行redatabinding来操作底层数据源。这一切都取决于…list.Items.add()不起作用,在绑定数据源之前,继续获取一个错误非常好的替代解决方案来将记录添加到datatable。
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);
}
}
}
}
}
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";