Solved

Making HashTable DataSource for Combobox in Windows Forms

Posted on 2006-06-16
7
1,799 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with error in Query 2 39
how to check group permission on a file located on a remote server ? 3 30
abstract class C# 1 36
Change to file doesn't show up 16 74
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.   …
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

735 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