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; }
}