将选定的值从一个列表框移动到另一个 [英] Moving selected values from one Listbox to another
问题描述
大家好,
我正在寻找一些有关在表单上填充2个列表框的帮助. Listbox1绑定到Dataview并显示从存储过程中选择的SQL数据:
Hi All,
I''m looking for some help with populating 2 Listboxes on my Form. Listbox1 is bound to a Dataview and shows SQL selected data from a Stored Procedure:
Dim Descriptions As DataView = New DataView(DS.Tables("Descriptions"))
ListBox1.DataSource = Descriptions
ListBox1.DisplayMember = "Product_Description"
ListBox1.ValueMember = "Product_Desc_ID"
ListBox1.SelectedIndex = -1
当我尝试以下代码时,它不会在Listbox2中显示Listbox1中的值,而只会显示"System.Data.DataRowView"
When i try the following code, it doesn''t display the value from Listbox1 in Listbox2, it only displays "System.Data.DataRowView"
'move one item from L1 to L2 ->
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If ListBox1.SelectedIndex < 0 Then
MsgBox("Please select an item to move from the Listbox on the left.", vbCritical, "ListBox1 Validation Error")
Else
If ListBox1.Items.Count > 0 Then
ListBox2.Items.Add(ListBox1.SelectedItem)
End If
End If
End Sub
另外,我需要能够将选定的值-Product_Desc_ID移到第二个Listbox2中,以及在Listbox2中显示来自Listbox1的文本.
谢谢
Also, i need to be able to move the selected value - Product_Desc_ID into the second Listbox2 aswell as displaying the the Text from Listbox1 in Listbox2.
Thanks
推荐答案
ListBox1.DisplayMember ="Product_Description"
ListBox1.ValueMember ="Product_Desc_ID"
ListBox1.DisplayMember = "Product_Description"
ListBox1.ValueMember = "Product_Desc_ID"
它仅显示"System.Data.DataRowView"
it only displays "System.Data.DataRowView"
因为您没有设置Display和Value成员.对象已移动,但根据在新列表框中设置的规则显示.
Because yo didn''t set the Display and Value members. The object is moved, but it''s shown according to the rules set in the new listbox.
此外,我需要能够将所选值-Product_Desc_ID移动到第二个Listbox2中,并在其中显示Listbox1中的文本列表框2.
Also, i need to be able to move the selected value - Product_Desc_ID into the second Listbox2 aswell as displaying the the Text from Listbox1 in Listbox2.
您正在-正在移动包含该数据的对象,只是在不告诉它如何呈现它.
You are - you''re moving the object that contains that data, you''re just not telling it how to render it.
克里斯蒂安,
感谢您的回复,但我有点困惑!我是否需要根据Listbox1中的值设置ListBox2.ValueMember和Listbox2.DisplayMember?
感谢您的帮助!
Hi Christian,
Thanks for your response but i''m a bit confused! Do i need to set both the ListBox2.ValueMember and Listbox2.DisplayMember according to the values from Listbox1?
Your help is appreciated!
假设第二个listbox
不受数据绑定,您不想自己添加ListBox1.SelectedItem
.如果要与SelectedItem
关联的ValueMember
,请尝试ListBox1.SelectedValue
.如果需要DisplayMember
,可以只使用ListBox1.Text
.
Assuming that you''re secondlistbox
is not data bound, you don''t want to add theListBox1.SelectedItem
by itself. TryListBox1.SelectedValue
if you want theValueMember
associated with theSelectedItem
. You can just useListBox1.Text
if you want theDisplayMember
.
这篇关于将选定的值从一个列表框移动到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!