I am using a listview, and trying to populate using a file.
I need the file to be read in as soon as the form starts.
private void mainForm_Load(object sender, EventArgs e)
{
//get file read in
if (File.Exists("../../MealDeliveries.txt"))
{
StreamReader sr = new StreamReader("../../MealDeliveries.txt");
//first line is delivery name
string strDeliveryName = sr.ReadLine();
do
{
//other lines
Delivery d = new Delivery(strDeliveryName, sr.ReadLine(), sr.ReadLine(), sr.ReadLine(), sr.ReadLine(), sr.ReadLine(), sr.ReadLine());
mainForm.myDeliveries.Add(d);
//check for further values
strDeliveryName = sr.ReadLine();
//stop if no more values
} while (strDeliveryName != null);
displayDeliveries();
}
}
private void displayDeliveries()
{
lstDeliveryDetails.Items.Clear();
foreach (Delivery d in mainForm.myDeliveries)
{
lstDeliveryDetails.Items.Add(d.DeliveryName);
}
}
The listview isn’t displaying anything, although the file is definately there!
Yes, it should assuming that the file is in the place you think it is. Are you positive that the file exists?
Try this. It will at least confirm whether or not the file is found.
Another thing to watch out for is reading lines in as a string to pass to your business object (Delivery). You might find it better to use some sort of serialization format provided by XmlSerializer or one of the file formats supported by Marcos Meli’s FileHelpers library. Either way, something more robust than reading in strings would be desirable.