保存到文本文件和dataGridView C#并加载

时间:2014-03-14 15:39:08

标签: c# datagridview openfiledialog savefiledialog databound

我需要能够将数据从dataGridView保存到.txt,然后才能将相同的数据加载到适当的位置。它是数据绑定。

这是我到目前为止的代码。 我可以保存到文件但是,它只会将文件中的第一条记录加载到dataGridView中。 非常感谢任何帮助!

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

3 个答案:

答案 0 :(得分:1)

您需要遍历文件中的所有项目

替换它:

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

有了这个:

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

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

答案 1 :(得分:0)

请尝试以下代码:

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

这将导致代码循环遍历文件,直到它到达空行(即文件结尾)。

这会将整个行放在CardNameNumBorrowed内。

为了使其正常工作,您需要为每个循环读取2行,如下所示:

string line;
string line2;

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

然后你可以更正你的代码:

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

或者,您可以更改文件格式,而不是每个信息有2行,您可以将其压缩到一行具有单独的值(即Jake^50其中^是分隔符)

然后,您需要修改代码以将行拆分为数组并从中拉取值。

答案 2 :(得分:0)

如果我有我的druthers,我写的文本文件用每行一条记录分隔,然后不用担心1条坏线会弄乱所有以下的行,但是对于你当前的结构,这应该有用了有一点错误检查。

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