How do I...

  • Thread starter Thread starter Bryan Dickerson
  • Start date Start date
B

Bryan Dickerson

I have a simple list box that I want to show a list of names and also I want
to store in the list box an id, just a string value, so that when the user
clicks on a name I can use the id to get information about that name. I
know how I would do it in VB6 (using the Tag property). Is there a better
way to do it in VB.Net?
 
Bryan Dickerson said:
I have a simple list box that I want to show a list of names and also I
want to store in the list box an id, just a string value, so that when the
user clicks on a name I can use the id to get information about that name.

\\\
Dim p As New Person()
p.Name = "Pink Panther"
p.Age = 22
Me.ComboBox1.Items.Add(p)

' Test.
MsgBox(DirectCast(Me.ComboBox1.Items(0), Person).ToString())
..
..
..
Public Class Person
Private m_Name As String
Private m_Age As Integer

Public Property Name() As String
Get
Return m_Name
End Get
Set(ByVal Value As String)
m_Name = Value
End Set
End Property

Public Property Age() As Integer
Get
Return m_Age
End Get
Set(ByVal Value As Integer)
m_Age = Value
End Set
End Property

Public Overrides Function ToString() As String
Return Me.Name & " (" & Me.Age.ToString() & ")"
End Function
End Class
///

Alternatively, you can use a bound combobox:

\\\
With Me.ListBox1

' This can be an 'ArrayList' or array too, for example.
.DataSource = Database.FindPeople(...)
.DisplayMember = "Name"
.ValueMember = "Age"
End With
///
 
When I do the Items.Add(object), how does the Listbox/Combobox know what
property to display?
 
Bryan Dickerson said:
When I do the Items.Add(object)

Use it if you want to add an object to the listbox.
how does the Listbox/Combobox know what property to display?

The listbox will display the value returned by the object's 'ToString'
method.
 
That would be great except that the object that I'm Adding to the listbox is
a DataRow. Can I override just a ToString method of that? If so, how?
 
Bryan,
That would be great except that the object that I'm Adding to the listbox
is a DataRow. Can I override just a ToString method of that? If so, how?
I doubt that, are you not binding a datatable or its defaultview or a
created dataview .

And than set the display and valuemember of those in the listboxbox?.

myListbox.datasource = mytable.defaultview 'although this defaultview is
overdone
myListbox.displaymember = "MyColumn1"
myListbox.displaymember = "MyColumn2"

I hope this helps,

Cor
 
Please bear with me as I'm trying very hard to learn, but do I 'set' the
DefaultView or is it pre-set? If I can set it, how do I do it?
 
Bryan,

It was confusing for me as well in the beginning.

Every datatable holds a dataview which is named the defaultview.
However you can use it to set by instance the sort order of a datatable in a
dataview

dt.defaultview.sort = "MySortColumn"

is the same as
dim dv as dataview = dt.defaultview
dv = "MySortColumn"
(and use than dv)

div dv as new dataview(dt)
dv = "MyOtherSortColumn"

Means that you use an extra dataview and can show the datatable on two
different controls in two different sort orders

I hope that this gives an idea

Cor
 
Ok, how do I specify which columns to include in the DataView? Oh, and
thanks for your patience with me!
 
Bryan,

Now you come in my eyes with a complete different question.

A defaultview and a dataview have a view on the complete table although
there is a rowfilter for showing.

This means as well all columns there is no column filter.

However there are a lot of way to choose the value from a column that you
want to use, and the other use use not.

I hope this helps,

Cor
 
I'm sorry but you lost me and my questions seem to be leading in the wrong
direction or leading off on unimportant tangents. If I have a simple
Dataset object, can I choose which columns make up a DataView or the
DefaultView? Or do all the columns in the Dataset automatically become part
of the dataview?
 
Bryan,
I'm sorry but you lost me and my questions seem to be leading in the wrong
direction or leading off on unimportant tangents. If I have a simple
Dataset object, can I choose which columns make up a DataView or the
DefaultView? Or do all the columns in the Dataset automatically become
part of the dataview?
I cannot answer this with yes or no because a defaultview is not direct
related to a dataset.
(Maybe helps this to become out of your confusion. A dataset is absolute not
the same as a recordset, A datatable looks a little bit as a recordset).

Dataset has a collection of datatables
DataTable as a collection of (data)columns and datarows
Datarow has a collection of Items which are described in the (data)columns

A datatable has integrated a dataview, which tells in what order or with
what filter they shoud be showed in complex controls.

Therefore in your problem where you have a Listbox you can go back to my
sample

myListbox.datasource = mytable
myListbox.displaymember = "MyColumn1"
myListbox.displaymember = "MyColumn2"

This shows a listbox with the text from Mycolumn1 from the table mytable.

Some people write this as well like this
dim dv as DataView = myTable.defaultview
or
dim dv as New Dataview(myTable) 'this creates a new dataview

Than the rest stays the same
myListbox.datasource = dv
myListbox.displaymember = "MyColumn1"
myListbox.displaymember = "MyColumn2"

What is for the rest in the table is not important for your problem.

I hope this helps,

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top