如何将项目从一个列表框添加到Windows应用程序中的另一个列表框 [英] how to add items from one listbox to another listbox in windows application

查看:83
本文介绍了如何将项目从一个列表框添加到Windows应用程序中的另一个列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有2个列表框.正在从SQL Server数据库中检索数据.在listbox1中,我要从此列表框中选择一些项目,然后通过添加按钮将它们添加到第二个项目.

问题:正在从数据库中检索值,但是当我从listbox1中选择一个值后单击添加按钮时,System.Data.DataRowView将自动显示在listbox2中.

Listbox2当前未连接到任何数据库.

以下是代码:

I have 2 listboxes in my application. The data is being retrieved from a SQL server database. In listbox1, I want to select few items from this listbox and add them to second one through add button.

PROBLEM: The values are being retrieved from the database but when I click the add button after selecting a value from listbox1, System.Data.DataRowView is displayed in listbox2 automatically.

Listbox2 is not currently connected to any databse.

Here is the code:

for (int i = 0; i < listBox1.Items.Count; i++)
               {
                   listBox2.Items.Add(listBox1.Items[i].ToString());
                   listBox1.Items.Remove(listBox1.Items[i].ToString());
               }

if anyone have any idea how to do it help me

推荐答案

请勿在项目上使用.ToString().尝试这样的事情:
Do not use .ToString() on items. Try something like this:
for (int i = 0; i < listBox1.Items.Count; i++)
{
    listBox2.Items.Add(listBox1.Items[i]);
}
listBox1.Items.Clear();


这应该是:
This should wotk:
for (int i = 0; i < listBox1.SelectedItems.Count; i++)
  {
      listBox2.Items.Add((ListViewItem)listBox1.SelectedItems[i].Clone());
      listBox1.Items.Remove(listBox1.SelectedItems[i]);
  }


for (int i = 0; i < listBox1.Items.Count; i++)
   {
       listBox2.Items.Add(listBox1.Items[i]);
   }
   listBox1.Items.Clear();


这篇关于如何将项目从一个列表框添加到Windows应用程序中的另一个列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆