Solved

Making HashTable DataSource for Combobox in Windows Forms

Posted on 2006-06-16
7
1,786 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Installing IIS after Visual Studio 2015 Community Edition 5 45
Linq Help 1 34
Unable to upgrade to .Net 4.6.2 on Server 2012r2 2 42
Replace &lt; with < 14 53
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A short film showing how OnPage and Connectwise integration works.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

948 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now