如何将数据保存为以后可以调用的格式

时间:2013-04-25 05:25:32

标签: c# datagridview datatable save rows

我一直致力于阅读文本文件并将文本组织成每个项目的块。我需要帮助找出在程序使用过程中可以保存和调用数据的方法。我试图将输出作为数据网格视图。我不知道如何处理这个问题。我想我可以为每个新块创建一个数据表,但我不知道该怎么做。

到目前为止我的代码:

public void findBlocks()
    {
        String fileName = "Drops.de";
        StreamReader streamReader = new StreamReader(fileName);
        String npcName;
        string npcID;
        string itemID;
        string itemAmount;
        string itemRarity;
        string itemName;

        while(!streamReader.EndOfStream){
            string line = streamReader.ReadLine();
            //start of block
            if(line.StartsWith("[")){
                npcName = line.TrimStart('[');
                npcName = npcName.TrimEnd(']');
                while(!line.StartsWith("[/")){
                    line = streamReader.ReadLine();
                    String[] s = line.Split(' ');
                    npcID = (s[0]);
                    itemName = (s[1]);
                    itemID = (s[2]);
                    itemAmount = (s[3]);
                    itemRarity = (s[4]);
                    DataTable dt = new DataTable();
                    DataGridViewRow dgv = new DataGridViewRow();

                    DataGridViewRow row = (DataGridViewRow)dataGridView1.Rows[0].Clone();
                    row.Cells[0].Value = itemName;
                    row.Cells[1].Value = itemID;
                    row.Cells[2].Value = itemAmount;
                    row.Cells[3].Value = itemRarity;
                    dataGridView1.Rows.Add(row);
                    dataGridView1.Refresh();
                    line = streamReader.ReadLine();
               //MessageBox.Show(npcName);
                }
            }
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        search();
    }

    public void loadItemList(string file)
    {
        String fileName = file;
        StreamReader streamReader = new StreamReader(fileName);

        while (!streamReader.EndOfStream)
        {
            string currentLine = streamReader.ReadLine();
            string s;
            String id;
            String itemName = "";

            if (currentLine.Contains("item"))
            {
                s = currentLine.TrimStart('i', 't', 'e', 'm', ' ', '=', ' ');
                id = s.Split('.')[0];
                itemName = id.Split('\t')[1];
                id = id.Split('\t')[0];

                listBox1.Items.Add(itemName + " " + id);

            }
            else
            {
                streamReader.ReadLine();
            }

        }
        streamReader.Close();
    }

这是我尝试排序的文本文件的示例:

[item1]
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000 7
[/item1]
[item2]
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000 7
[/item2]
[item3]
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000 7
[/item3]

等。

非常感谢任何和所有帮助:)

1 个答案:

答案 0 :(得分:1)

可以找到示例帖子here,下面列出了关键步骤:

  1. 使用xml保存数据
  2. 将数据从xml读入列表
  3. 然后将其绑定到您的网格视图。
  4. 示例代码可能是:

    [Serializable]
    class SomeProperty
    {
        public int Count1 {get;set;}   //2550
        public int Count2 {get;set;}   //995
        public int Count3 {get;set;}   //200000
        public int Count4 {get;set;}   //7
    }
    
    List<SomeProperty> objects=new List<SomeProperty>();
    objects.Add(...)
    
    //Saving
    XmlSerializer x = new XmlSerializer(objects.GetType());
    using (FileStream stream = System.IO.File.Create(FilePath))
    {
        XmlWriter writer = XmlWriter.Create(stream);
        x.Serialize(writer, objects);
    }
    
    //Reading
    XmlSerializer x = new XmlSerializer(typeof(List<SomeProperty>));
    using (FileStream fs = new FileStream(FilePath, FileMode.Open))
    {
        XmlReader reader = new XmlTextReader(fs);
        var objects= (List<SomeProperty>)x.Deserialize(reader);
    }