DataSource at design time

S

Stephen

IN VB.NET
I have a form with a ListBox and would like to set the DataSource property
at design-time but I can't seem to declare any kind of variable that will
show up in the list. I have tried a traditional array, ArrayList,
Collection as public vars in the Form Class's Declarations section. Setting
the DataSource property to any of these works fine at run time.

I want to do it at design time because after I change the Collection, it
doesn't update it in the ListBox, even though breaking showed that the
Collection.Count property had gone up. I first add to it in code with
Coll.Add("WHATEVER") and it shows up fine, before and after binding. But I
try
to do this: Coll.Add(TextBox1.Text) and it doesn't show up in the ListBox
but is apparently being added to the collection object just fine.

Any answer to either of these questions would be helpful
Thanks,
Stephen
 
J

Jay B. Harlow [MVP - Outlook]

Stephen,
At design time the only 'native' collection that I know of you can use is a
DataTable (specifically a DataTable in a DataSet.)

You can then add new rows to the DataTable's Rows collection and they will
show up in the List.

The reason adding items to the ArrayList do not show up in the ListBox is
that the ArrayList does not support the ListChanged event (part of the
IBindingList interface).

Is there a reason you want to bind to a DataSource as opposed to simply
adding the items to the ListBox.Items collection (via the ListBox.Items.Add
method)?

Hope this helps
Jay
 
S

Stephen

Simplicity, speed and the guarantee that the data is exactly the same. .NET
is supposed to make for rapid app development after all. Is there a way to
refresh the ListBox? I tried rebinding it after the data changed but it
didn't work. Maybe I have to unbind it then rebind it.
 
K

Kevin Yu

Hi Stephen,

Since the refresh of ListBox doesn't work when items are added or removed
from the arraylist, the simplest workaround is to reset the DataSource
property of the ListBox. you can try the following code to achieve this.

arraylist.Add("Whatever")
listbox.DataSource = Nothing
listbox.DataSource = arraylist

You cannot omit the second line, because if you simply set the
listbox.DataSource to arraylist, the listbox will check if the new
DataSource is the same as the old one. (The listbox doesn't know that the
arraylist has changed, it only knows that they are the same object
reference.)

Does this answer your question? If anything is unclear, please feel free to
reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

--------------------
| From: "Stephen" <[email protected]>
| References: <#[email protected]>
<[email protected]>
| Subject: Re: DataSource at design time
| Date: Thu, 2 Oct 2003 23:37:35 -0400
| Lines: 59
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <ePMyu#[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.vb
| NNTP-Posting-Host: c-66-177-177-97.se.client2.attbi.com 66.177.177.97
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:143316
| X-Tomcat-NG: microsoft.public.dotnet.languages.vb
|
| Simplicity, speed and the guarantee that the data is exactly the same.
..NET
| is supposed to make for rapid app development after all. Is there a way
to
| refresh the ListBox? I tried rebinding it after the data changed but it
| didn't work. Maybe I have to unbind it then rebind it.
|
message
| | > Stephen,
| > At design time the only 'native' collection that I know of you can use
is
| a
| > DataTable (specifically a DataTable in a DataSet.)
| >
| > You can then add new rows to the DataTable's Rows collection and they
will
| > show up in the List.
| >
| > The reason adding items to the ArrayList do not show up in the ListBox
is
| > that the ArrayList does not support the ListChanged event (part of the
| > IBindingList interface).
| >
| > Is there a reason you want to bind to a DataSource as opposed to simply
| > adding the items to the ListBox.Items collection (via the
| ListBox.Items.Add
| > method)?
| >
| > Hope this helps
| > Jay
| >
| > | > > IN VB.NET
| > > I have a form with a ListBox and would like to set the DataSource
| property
| > > at design-time but I can't seem to declare any kind of variable that
| will
| > > show up in the list. I have tried a traditional array, ArrayList,
| > > Collection as public vars in the Form Class's Declarations section.
| > Setting
| > > the DataSource property to any of these works fine at run time.
| > >
| > > I want to do it at design time because after I change the Collection,
it
| > > doesn't update it in the ListBox, even though breaking showed that the
| > > Collection.Count property had gone up. I first add to it in code with
| > > Coll.Add("WHATEVER") and it shows up fine, before and after binding.
| But
| > I
| > > try
| > > to do this: Coll.Add(TextBox1.Text) and it doesn't show up in the
| ListBox
| > > but is apparently being added to the collection object just fine.
| > >
| > > Any answer to either of these questions would be helpful
| > > Thanks,
| > > Stephen
| > >
| > >
| >
| >
|
|
|
 
J

Jay B. Harlow [MVP - Outlook]

Stephen,
Simplicity, speed and the guarantee that the data is exactly the same. ..NET
is supposed to make for rapid app development after all.
Huh? Based on the above it sounds like you want to use ListBox.Items. It is
simply, it is speedy. I'm not sure what you are comparing to be exactly the
same.
Is there a way to refresh the ListBox?
Yes binding to a class that implements IBindingList such as DataTable
(DataView) will refresh the ListBox automatically.

Also using the ListBox.Items collection instead of an ArrayList will cause
the ListBox to be refreshed.

I have not tried Kevin's code I would expect Kevin's code to also refresh
the ListBox, for the reason's Kevin identified.

Hope this helps
Jay
 

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

Top