在DataGrid中编辑单元格后将数据保存到文件

时间:2019-11-13 11:42:45

标签: c# wpf

在DataGrid中编辑单元格后,我试图将数据保存到平面文件中

private void DG1_CellEditEnding(object sender, 
        DataGridCellEditEndingEventArgs e)
    {
        Save();
    }

private void Save()
    {
        try
        {
            File.Delete(filename);
        }
        catch(IOException ioe)
        {
            Console.WriteLine(ioe.StackTrace);
        }
        FileStream fileStream = File.OpenWrite(filename);
        string x = "";
        foreach (Asset asset in room.Assets)
        {
            x += asset.Name + "," + asset.Type + "," + asset.Tag + "," + asset.SerialNumber + "\n";
        }

        fileStream.Write(Encoding.ASCII.GetBytes(x), 0, x.Length);
        fileStream.Close();
    }

问题在于它保存了单元格的旧值。如果我编辑两次,则可以更改该值,但这是不希望的。

0 个答案:

没有答案