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

CSV data 2

Status
Not open for further replies.

RascaPR

Technical User
Oct 27, 2007
26
0
0
US
Hello all,

I am running into a dead end. I have a comma separated value file (airports.txt) which has 2 columns in this format and is delimited by a PIPE "|":

AALBORG, DENMARK|AAL
AARHUS, DENMARK|AAR
AL AIN, UNITED ARAB EMIRATES|AAN
ALBACETE, SPAIN|ABC
ABILENE, TX|ABI

How can I read this file and be able to only insert all the data in column 1, into one combo box and column 2 into a different combo box?
 
This better not be a school project...

//Create an airport class.
public class Airport
{
public string City;
public string Country;
public string Code;

public Airport(string city, string country, string code)
{
City = city;
Country = country;
Code = code;
}
}


//Put this method where you want to get your list of airports
public List<Airport> GetAllAirports(string filepath)
{
List<Airport> airports = new List<Airport>();

TextReader reader = new StreamReader(filepath);
string line = "";

while ((line = reader.ReadLine()) != null)
{
string[] columns = line.Split(',');
string[] data = columns[1].Split('|');

airports.Add(new Airport(columns[0], data[0], data[1]));
}

reader.Close();
reader.Dispose();

return airports;
}


Now you will have a list of Airport classes. You can loop through all the airports in your list and add them to combo boxes etc at your own will.

Enjoy.

 
Thanks! Worked like a charm... And no, it's not a school project! lol I am trying to learn C#, so I have started on a flight simulation related program for my Virtual Airline.

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top