How to Get a List of All Countries/Cities to Populate a Listbox

Where can I get a list of all countries/cities to populate a listbox?

Try this list. Link was updated to the new location.

https://www.iso.org/obp/ui/#iso:pub:PUB500001:en

How to populate a dropdownlist with all countries?

Update 8/9/2016:

List of Countries


Just did a quick search and found this site:

http://madskristensen.net.web7.reliabledomainspace.com/post/XML-country-list.aspx.

Here's the direct link to the file:

http://cid-247fb0008340dbcd.office.live.com/self.aspx/workstion/countries.xml

Update: Code to populate your drop down list with the list of countries:

Dim doc = XDocument.Load("path to url\file")

Dim countries = From c in doc.Descendants("country")
Select c.Value

For Each country In countries
DropDownList.Add(country)
Next

DropDownList.DataBind()

MVC 4 ListBox populated with another ListBox

In the home controller I have decided to use the collection initializer to build the list of countries but more importantly I felt the code was cleaner by using the ViewBag dynamic over ViewData.

public ActionResult Index()
{
var countries = new List<string> {"USA", "UK", "India"};
var countryOptions = new SelectList(countries);
ViewBag.Countries = countryOptions;
return View();
}

Next is the GetStates() action method. Here I have made one change that enables me to retrieve the states over a HttpGet request. The reason for this is I believe that HttpGet is the best fit for this request as we are simply retrieving information form the server. If we were adding or updating states then a HttpPost request would be required.

  public JsonResult GetStates(string country)
{
var states = new List<string>();
switch (country)
{
case "USA":
states.Add("California");
states.Add("Florida");
states.Add("Ohio");
break;
case "UK":
states.Add("London");
states.Add("Essex");
break;
case "India":
states.Add("Goa");
states.Add("Punjab");
break;
}

//Add JsonRequest behavior to allow retrieving states over http get
return Json(states, JsonRequestBehavior.AllowGet);
}

The second, and final, part of my solution is the Index.cshtml file. In this file I have the html for the form as well as the javascript required to retrieve the states from the server.

@using (Html.BeginForm())
{
<div>Select country:</div>
<div>@Html.DropDownList("country",
ViewBag.Countries as SelectList,
"Please select",
new { id = "country" })
</div>
<div>Select state:</div>
<div>
<select id="state" disabled="disabled"></select>
</div>
<input type="submit" value="Submit"/>
}


@section scripts
{
<script type="text/javascript">
$(function() {
$('#country').on('change', function() {
var stateDropdown = $('#state');
//disable state drop down
stateDropdown.prop('disabled', 'disabled');
//clear drop down of old states
stateDropdown.empty();

//retrieve selected country
var country = $(this).val();
if (country.length > 0) {
// retrieve data using a Url.Action() to construct url
$.getJSON('@Url.Action("GetStates")', {
country: country
})
.done(function (data) {
//re-enable state drop down
stateDropdown.removeProp('disabled');
//for each returned state
$.each(data, function (i, state) {
//Create new option
var option = $('>option /<').html(state);
//append state states drop down
stateDropdown.append(option);
});
})
.fail(function (jqxhr, textStatus, error) {
var err = textStatus + ", " + error;
console.log("Request Failed: " + err);
});
}
});
})
</script>
}

Is there somewhere a Yaml or SQL insert list of all countries?

I believe this is what you're looking for: http://download.geonames.org/export/dump/

How to find a list of countries?

There are also web services that provide this type of data, so perhaps you could periodically make use of a web service call and store it to local data store (e.g. a database or XML).

Is there any method to fill the combobox in winforms with all country names and another combobox with cities of the selected country?

Sure there is a procedure.
You could start with a simple data structure:

public class Country
{
public string Name { get; set; }
public IList<City> Cities { get; set; }

public Country()
{
Cities = new List<City>();
}
}

public class City { public string Name { get; set; } }

Then, instantiate this structure, e.g. into a property of your form...

Countries =
new List<Country>
{
new Country
{
Name = "Germany",
Cities =
{
new City {Name = "Berlin"},
new City {Name = "Hamburg"}
}
},
new Country
{
Name = "England",
Cities =
{
new City {Name = "London"},
new City {Name = "Birmingham"}
}
}
};

In your form, instantiate two Binding Sources (BS):

  • The first BS binds to the Countries
    property.
  • The second BS binds to the first (DataSource = firstBS) and its DataMember should be "Cities".

Now you need two dropdowns:

  • 1st: DataSource = first BS, DisplayMember = "Name"
  • 2nd: DataSource = second BS, DisplayMember = "Name"

and you should be pretty much done.

populate listbox from list of objects

You can use String.Format to concatenate the first name and surname while adding ListBoxItem in loop. You can use listbox DoubleClick event to show details by finding the SalesmanDetails on DoubleClick. the following code is sample:

    private void btnViewAll_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
foreach (SalesmanDetails details in salesmanList)
{
listBox1.Items.Add(String.Format("{0} {1}",details.firstName,details.surname));
}
}

private void listBox1_DoubleClick(object sender, EventArgs e)
{
int SalesmanDetailsIndex = listBox1.SelectedIndex;
SalesmanDetails selectedSalesman=salesmanList[SalesmanDetailsIndex];
MessageBox.Show(String.Format("{0} {1} email {2}",selectedSalesman.firstName,selectedSalesman.surname,selectedSalesman.email));
}


Related Topics



Leave a reply



Submit