1

I am needing to have the ability to save data from a dataGridView to a .txt and then be able to load the same data back in to the appropriate spots. It is databound.

Here is the code that I have so far. I can save to file but, it will only load the first record in the file into the dataGridView. Any help is greatly appreciated!

private void LoadButton_Click(object sender, EventArgs e)
    {
        if (openFileDialog1.ShowDialog().Equals(DialogResult.OK))
        {
            cardlist = new List<Card>();
            using (System.IO.StreamReader file = new System.IO.StreamReader(openFileDialog1.FileName))
            {
                Card newcard = new Card();
                newcard.CardName = file.ReadLine();
                newcard.NumBorrowed = Convert.ToInt32(file.ReadLine());
                cardlist.Add(newcard);
            }
            dataGridView1.DataSource = cardlist;

        }
    }

    private void SaveButton_Click(object sender, EventArgs e)
    {
        if (saveFileDialog1.ShowDialog().Equals(DialogResult.OK))
        {
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(saveFileDialog1.FileName))
                foreach (Card currentCard in cardlist)
                {
                    file.WriteLine(currentCard.CardName);
                    file.WriteLine(currentCard.NumBorrowed);
                }
        }
    }
    public class Card
    {
        public String CardName { get; set; }
        public int NumBorrowed { get; set; }
    } 
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
BPratt93
  • 11
  • 2
  • You're only reading in two lines of your file in your LoadButton_Click function. Try looping until you get to the end of the file, similar to what you do in your SaveButton_Click function. – B L Mar 14 '14 at 15:42
  • Was gonna say the same thing, I was looking at this thinking "I'm pretty sure there's meant to be another foreach loop there....hmmmm" – JBurnham Mar 14 '14 at 15:43
  • Thank you both so much, I have spent the past 2 weeks working on this personal windows app and my brain is fried. I should have noticed that small detail. Thanks for the help! – BPratt93 Mar 14 '14 at 15:47

3 Answers3

1

You need to iterate over all items in the file

Replace This:

using (System.IO.StreamReader file = new System.IO.StreamReader(openFileDialog1.FileName))
{
    Card newcard = new Card();
    newcard.CardName = file.ReadLine();
    newcard.NumBorrowed = Convert.ToInt32(file.ReadLine());
    cardlist.Add(newcard);
}

With This:

int lineCount=0;
string line=string.Empty;
using (System.IO.StreamReader file = new System.IO.StreamReader(openFileDialog1.FileName))
{
    Card newcard = new Card();
    while((line=file.ReadLine()) != null)
    {
    if(lineCount == 0)
    {
     newcard.CardName = line;
     lineCount = 1;
    }
    else if(lineCount == 1)
    {
     newcard.NumBorrowed = Convert.ToInt32(line);
     lineCount = 0;
    }
     cardlist.Add(newcard);
}

OR

int i=0;
foreach(var line in File.ReadLines(openFileDialog1.FileName))               
{                
    Card newcard = new Card();
    if(i==0)
    {
         newcard.CardName = line;
         i = 1;
    }
    else if(i==1)
    {
         newcard.NumBorrowed = Convert.ToInt32(line);              
         i=0;
    }
    cardlist.Add(newcard);
}
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
0

Try the following code :

using (System.IO.StreamReader file = new System.IO.StreamReader(openFileDialog1.FileName))
{
    string line;

    while ((line = file.ReadLine()) != null)
    {
        Card newcard = new Card();
        newcard.CardName = line;
        newcard.NumBorrowed = Convert.ToInt32(line);
        cardlist.Add(newcard);
    }
}

This will cause the code to loop through the file until it reaches a null line (i.e. End of file).

This will place the entire line inside both CardName and NumBorrowed.

For this to work correctly you will either need to read 2 lines per loop, like so :

string line;
string line2;

while ((line = file.ReadLine()) != null && (line2 = file.ReadLine()) != null) {
}

And then you can correct your code :

Card newcard = new Card();
newcard.CardName = line;
newcard.NumBorrowed = Convert.ToInt32(line2);
cardlist.Add(newcard);

Alternatively, you could change your file Format and instead of having 2 lines per info, you could condense it onto one line with a seperating value (i.e. Jake^50 where ^ is the seperator)

You then need to just modify the code to split the line into an array and pull the values from that.

Nunners
  • 3,047
  • 13
  • 17
0

If i had my druthers, I'd write my text file delimited with one record per line and then not worry that 1 bad line will mess up all the following lines, but with your current structure this should work and it has a little error checking.

        using (StreamReader reader = new StreamReader(openFileDialog1.FileName))
        {
            bool stillReading = true;
            while (stillReading)
            {
                string card, numBorrowed;
                card = reader.ReadLine();
                numBorrowed = reader.ReadLine();
                if (card != null && numBorrowed != null)
                {
                    int numB;
                    Card newcard = new Card
                    {
                        CardName = card,
                        NumBorrowed = Int32.TryParse(numBorrowed, out numB) ? numB : 0
                    };
                    cardlist.Add(newcard);
                }
                else
                {
                    stillReading = false;
                }
            } 
        }
safetyOtter
  • 1,430
  • 14
  • 26