Solved

Making HashTable DataSource for Combobox in Windows Forms

Posted on 2006-06-16
7
1,803 Views
Last Modified: 2008-01-09
Hi, I have the following information:

Dim monthTable As New Hashtable
        monthTable.Add(1, "January")
        monthTable.Add(2, "February")
        monthTable.Add(3, "March")
        monthTable.Add(4, "April")
        monthTable.Add(5, "May")
        monthTable.Add(6, "June")
        monthTable.Add(7, "July")
        monthTable.Add(8, "August")
        monthTable.Add(9, "September")
        monthTable.Add(10, "October")
        monthTable.Add(11, "November")
        monthTable.Add(12, "December")

       The following does not work.
        ComboBox1.DataSource = monthTable

How do I make the HashTable the datasource of my combobox so I can access the key or values. I was reading that HashTable is not part of List or Ilist and hence, it cannot be a datasource. What is the workaround? Has anybody tried something like this. I'm sure it's simply for you guys.

Sanna

0
Comment
Question by:Sanmarie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 4

Assisted Solution

by:fffej78
fffej78 earned 40 total points
ID: 16922787
Not the answer you wanted, but why don't you just use a string array.  Not sure on the syntax for VB.NET, but in C# I'd just do

String[] months = new String[]{"January","Feb","Mar" }; //and so on!

ComboBox1.DataSource = months;

There is little point using a map here when the keys are sequential integers.  The mapping from 1-12 to 0-11 shouldn't pose any issues :)
0
 

Author Comment

by:Sanmarie
ID: 16922867

Thanks but I was hoping to avoid this.
0
 
LVL 4

Expert Comment

by:fffej78
ID: 16922883
Why?  What is the rationale for using a map?  What is it buying you?
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

Author Comment

by:Sanmarie
ID: 16923018

I'm not saying your answer is wrong. There are many ways of doing something. In asp.net, you can set a hashtable as the datasource, I'm just wondering if there is something similar in windows forms. That's all.
0
 
LVL 11

Expert Comment

by:Babycorn-Starfish
ID: 16926683
Hi,

what exactly are you trying to achieve?, i'm just asking so i can try and think of a workaround.

Cheers

BCS
0
 

Author Comment

by:Sanmarie
ID: 16926811
I'm building a so called 'crosstab' datagrid user control and depending on the month that is selected from a combobox, I want to do some stuff. So, I will be displaying the month name in the combobox and behind the scenes I will be using the index of the month. Example, January is index 0. However, there are if cases where if the month is at a particular index then I want to select another month at another index. These things I won't know until runtime. Here, I will have to use for loops to determine what I want. I was trying to avoid the for loops but it seems like it's a crime to do so.

Since I did something similar in asp.net using a hashtable, I just have it in the back of my mind that maybe there is an alternative in windows forms than using arrays. I guess there isn't an alternative.

The previous answer suggested one way. He did not state that it was the only way. If this is the only way, then I will accept it as an answer.
0
 
LVL 11

Accepted Solution

by:
Babycorn-Starfish earned 85 total points
ID: 16927567
Hi there,

do you actually use the hashtable anywhere else? it just that it's a bit expensive given that it's so sparsely populated. Unless you absolutely need to use the hashtable i'd suggest using one of the collections that implements IList, such as an Array or ArrayList or simply add the items to your combo. Alternatively you could create a custom hashtable with an IList although i'd steer clear of this.

BCS
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

742 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question