Reputation: 11
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; }
}
Upvotes: 1
Views: 1960
Reputation: 1460
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;
}
}
}
Upvotes: 0
Reputation: 3047
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.
Upvotes: 0
Reputation: 26209
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);
}
Upvotes: 1