0

I am doing a winforms applications, that identify vehicle.

In this application, I will use a identification code that correspond to a vehicle.

It is simple, i only have one code for all cars for a year, and one code for bike in a year.

So, I was thinking about this kind of datas ::

year | car code | bike code
2014 | 156 | 185
2015 | 158 | 189

I have a subform, that will show all this data into a listbox(or another list type), and also, allow me to add a line. When I will add a line, I will be displayed in the list, and will be add into the file.

And in another view, I will displays all the 'year' part into a combo box.

What I want to know is , what is the best way to store my data into a file (csv, xml, or others...) so that I can read and edit easily this file, and displays the datas into a

I know how to do with a database, but without, I never done it, and don't find anything that can help me.

I hope I'm clear, if not, tell me.

edit :

public FormPref()
        {

            lst = GetIdentifiant(path);
            dataGridViewIdentifiant.DataSource = lst;
}

private void buttonAddIdentifiant_Click(object sender, EventArgs e)
        {
            Vehicule identifiant = new Vehicule();
            if (!string.IsNullOrEmpty(textBoxYear .Text) &&
                !string.IsNullOrEmpty(textBoxCarCode .Text) &&
                !string.IsNullOrEmpty(textBoxBikeCode .Text))
            {
                identifiant.Year = textBoxYear .Text;
                identifiant.CarCode = textBoxCarCode .Text;
                identifiant.BikeCode = textBoxBikeCode .Text;
                lst.Add(identifiant); 
            }
        }

I tried update the display in the datagridview with refresh, update, but nothing works.

The datagridview is bind with the value of the class.

Thank you.

provençal le breton
  • 1,428
  • 4
  • 26
  • 43
  • here is a comprehensive answer http://stackoverflow.com/questions/16352879/write-list-of-objects-to-a-file – Kevin Roche Jul 03 '14 at 09:23
  • For such a simple data set I'd probably go with csv. It's easy to edit, write, parse and has little overhead. – Herman Jul 03 '14 at 09:29

1 Answers1

1

If you don't have a lot of data, you can store it in a csv file. When adding new values you can use File.AppendAllLines to avoid rewriting the file

Quick and dirty

public class Vehicule
{
    public int Year { get; set; }
    public int CarCode { get; set; }
    public int BikeCode { get; set; }

    public override string ToString()
    {
        return string.Format("{0},{1},{2}", Year, CarCode, BikeCode);
    }
}

public class FileStorage
{
    public List<Vehicule> GetVehicules(string filePath)
    {
        return File.ReadAllLines(filePath).Select(s => s.Split(',')).Select(split => new Vehicule
        {
            Year = int.Parse(split[0]),
            CarCode = int.Parse(split[1]),
            BikeCode = int.Parse(split[2])
        }).ToList();
    }

    public void WriteVehicules(string filePath, IEnumerable<Vehicule> vehicules)
    {
        File.WriteAllLines(filePath, vehicules.Select(s => s.ToString()));
    }
}

There is also very good nuget packages to easily manipulate CSV ;)

alexandrekow
  • 1,927
  • 2
  • 22
  • 40