将一个列表框值移动到另一个 [英] moving one listbox value to another

查看:129
本文介绍了将一个列表框值移动到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我在后端使用VS2008和SQL Server 2008.我正在创建一个Windows应用程序,其中有2个列表框.正在从listbox1的SQL Server数据库中检索数据.我想从此列表框中选择几个itms并通过添加按钮将它们添加到第二个列表框中.问题:正在从数据库中检索值,但是当我从listbox1中选择一个值后单击添加按钮时,System.Data.DataRowView将自动显示在listbox2中. listbox2当前未连接到任何数据库.这是代码

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

我也尝试过使用此方法:

for(int i = 0; i< from.SelectedItems.Count; i ++)
{to.Items.Add(from.SelectedItems [i] .ToString()); }
foreach(新ArrayList(from.SelectedItems)中的可变项)
{from.Items.Remove(item); }

from和to分别是listbox1和listbox2

Hi ,
I m using VS2008 with SQL server 2008 at the backend. I m creating a windows application in which I have 2 listboxes.the data is being retrieved from SQL server database in listbox1.i want to select few itms from this listbox and add them to second one through add button. PROBLEM: the values are being retireved from database but when i click 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]);
listBox1.Items.Remove(listBox1.SelectedItem);
}

i have also tried using this:

for (int i = 0; i < from.SelectedItems.Count; i++)
{ to.Items.Add(from.SelectedItems[i].ToString()); }
foreach (var item in new ArrayList(from.SelectedItems))
{ from.Items.Remove(item); }

from and to are listbox1 and listbox2 respectively

any help will be appreciated

推荐答案

我相信您想将所选项目添加到listbox2并将其从listbox1中删除,以下内容将有所帮助..


I belive you wanted to add the selected item to the listbox2 and remove it from listbox1 and the following should help..


try
          {
              listBox2.Items.Add(listBox1.SelectedItem);
              listBox1.Items.Remove(listBox1.SelectedItem);

          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
          }


这篇关于将一个列表框值移动到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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