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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DropList

Status
Not open for further replies.

pasu

Programmer
Aug 9, 2003
17
US
I am working on a C# dot net web page, I am working on a Customer information page with a droplist of States, data is pulling from a table of US States. When I load the data from the customer table I will set the DropList Selected Value equal to the Customer record's state, however, some of the State is non US state, for example, Canada state, in that case the page get error message saying that the range is out of bound. Is there anyway that I can check if the record's state is one of the state in the DropList? Do I have to do a for / while loop to go thur the dataset of the US State? Thanks!

Patrick
 
Remember that a DropDown list is just a View of your data. You could very easily put your data into a hashtable and do a quick query for the name and index value.

HashTable statehash = new HashTable();

void PopulateList(string[] states)
{
foreach (string state in states)
{
dropdownlist1.Add(state);
statehash.Add(state, dropdownlist1.Items.Count - 1);
}
}

void FindItemInList(string item)
{
if (statehash.Contains(item))
{
//select the correct index for that item
}
else
{
//A different item was requested
}
}
 
Thank you! It works, and I just find out there is a method with the DropDownList to do that:

if (DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText(var_state))==-1)
// then the var_state is not in the DropDownList.

Patrick

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top