Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data Binding with a Hashtable

Status
Not open for further replies.

Echilon

Programmer
Feb 22, 2007
54
GB
I have a form which is used to add, delete and edit entries containing server details for use in my application. Currently, the values entered into the text boxes on the form are saved when the 'save server' button is clicked, and erased when the 'new server' button is clicked. The servers are displayed in a list box, and when a server is clicked, the fields are updated to show the saved details.

The details are populated from and saved to a Hashtable of Server objects.

I've been looking into data binding, but I can't figure out how to get it to work. There's no database involved, only the hashtable. I could use a dictionary or another type of set if that would make things easier, but what I'd like ideally (if I understand data binding correctly) is for the display of server details to be populated from the data source.

Is what I'm looking for possible?
 
I don't think its' possible with a hashtable. You could create a "Server" class and use a List<Server> along with a bindingsource though. The selected index in the list control will correspond with the object you need from your list, and voila you have an object to get all your details from.

You could also override the Equals method in your Server class to make the list more easily searchable (in case you want to order the objects differently in the ListBox).
You could also use a DataTable but those are a bit clunky to work with if you aren't filling them from a database. I'd go with the list in this case.

Hope this helps,

Alex

[small]----signature below----[/small]
Majority rule don't work in mental institutions

My Crummy Web Page
 
I think it is possible to databind to a hash table.

Something like this should work

Code:
Hashtable myHashTable = new HashTable();

myHashTable.Add ("First", "The first item");
myHashTable.Add ("Second", "The second item");
myHashTable.Add ("Third", "The third item");
myHashTable.Add ("Fourth", "The fourth item");
    
ddlMyDropDownList.DataSource = myHashTable.Keys;
ddlMyDropDownList.DataBind();

Charlie Benger-Stevenson
Hart Hill IT Ltd
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top