C# 如何将Winform Listbox1中的项目集合移动到Listbox2?
我正在用MySQL(C#)以win形式为学院做学生出勤项目 我已经问过了,我找到了解决办法 更新的查询-C# 如何将Winform Listbox1中的项目集合移动到Listbox2?,c#,winforms,C#,Winforms,我正在用MySQL(C#)以win形式为学院做学生出勤项目 我已经问过了,我找到了解决办法 更新的查询- string MyConString = ConfigurationManager.ConnectionStrings["College_Management_System.Properties.Settings.cmsConnectionString"].ConnectionString; MySqlConnection connection = new M
string MyConString = ConfigurationManager.ConnectionStrings["College_Management_System.Properties.Settings.cmsConnectionString"].ConnectionString;
MySqlConnection connection = new MySqlConnection(MyConString);
string cnd1 = "select name,admin_no from student_admision_master where course='" + course_code + "' AND year='" + year_code + "' AND sem='" + semester_code + "' AND batch='" + batch_code + "'";
MySqlDataAdapter da = new MySqlDataAdapter(cnd1, connection);
connection.Open();
DataSet ds = new DataSet();
MySqlCommand command = connection.CreateCommand();
da.Fill(ds, "student_admision_master");
//dataGridView1.DataSource = ds.Tables[formName];
listBox1.DataSource = ds.Tables[0].DefaultView;
listBox1.DisplayMember = "name";
listBox1.ValueMember = "admin_no";
connection.Close();
我在列表框中找到了学生的名字。但是Listbox到Listbox移动代码抛出错误
//所有Listbox2项目r都移动到ListBox3
private void btn_toOd_Click(object sender, EventArgs e)
{
int count = listBox2.Items.Count;
for (int i = 0; i < count; i++)
{
listBox3.Items.Add(listBox2.Items[i].ToString());
}
listBox2.Items.Clear();
}
private void btn\u toOd\u Click(对象发送方,事件参数e)
{
int count=listBox2.Items.count;
for(int i=0;i
//所有Listbox3项都被移动到ListBox2
私有void btn_from od_Click(对象发送者,事件参数e)
{
int count=listBox3.Items.count;
for(int i=0;i private void btn_toAb_Selected_Click(object sender, EventArgs e)
{
int count = listBox1.SelectedItems.Count;
for (int i = 0; i < count; i++)
{
listBox2.Items.Add(listBox1.SelectedItems[i].ToString());
}
for (int i = 0; i < count; i++)
{
listBox1.Items.Remove(listBox1.SelectedItems[0]);
}
}
private void btn_fromAb_Selected_Click(object sender, EventArgs e)
{
int count = listBox2.SelectedItems.Count;
for (int i = 0; i < count; i++)
{
listBox1.Items.Add(listBox2.SelectedItems[i].ToString());
}
for (int i = 0; i < count; i++)
{
listBox2.Items.Remove(listBox2.SelectedItems[0]);
}
}
private void btn\u toAb\u Selected\u单击(对象发送方,事件参数e)
{
int count=listBox1.SelectedItems.count;
for(int i=0;i
当DataSource属性为时,无法修改Items集合
设置在参数异常中
如何将Listbox1中的项目集合移动到Listbox2
以及如何从列表框中访问特定的ValueMember(管理员编号)
bzs我想根据管理员编号保存记录
信息是否足够或者我可以在这里添加图片
请给出一个这样做的想法
提前感谢….您的列表框是数据绑定的-这意味着列表框中的项目由绑定管理器自动管理。你不能用这种方式移动物品 相反,您需要相应地修改基础数据源。这意味着:不要在列表框中添加/删除项目,而是在基础数据集中添加/删除项目 列表框中的每个项目都是一个
DataRow
或DataRowView
,它有一个Row
属性来访问底层的DataRow
。一旦访问了数据行
,就可以使用行['fieldname']
确定字段值
要将行从一个数据集移动到另一个数据集,必须使用目标表上的ImportRow
方法添加行。然后必须从源表中删除该行。示例(您可能需要使用它,因为我现在还没有尝试过):
我可以用两列列表框吗?。在这种情况下,移动数据是可能的?。请给出一个建议……你所说的“两列列表框”是什么意思?它是一个列表框,每个学生有一行,但有两列显示例如名字和姓氏?实际上,我认为这并不重要:您可以确定列表中所选项目的列表。然后可以确定数据集中的相应行。所以基本上是一样的。
DataRow row = listBox.SelectedItem;
/* The above may need to be changed to
DataRow row = (listBox.SelectedItem as DataRowView).Row;
in case the SelectedItem is a DataRowView instead of a DataRow
*/
destDataSet.Table.ImportRow(row);
sourceDataSet.Table.Remove(row);