Solved

Making HashTable DataSource for Combobox in Windows Forms

Posted on 2006-06-16
7
1,783 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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 to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

8 Experts available now in Live!

Get 1:1 Help Now