首页 > 解决方案 > 我需要序列化它以保存到文件吗?

问题描述

我有一个从 .json 文件反序列化并绑定到 datagridview 的数据表。

我正在通过 UI 进行一些小的更改,将列中的值减去 1 或将值加 1。

所以在这种情况下,我没有反序列化到这里,只是读取数据表并对数据表进行了更改。现在我已经完成了它,令人惊讶的是,当我关闭应用程序时数据并没有存储。

我可以绑定数据并完成它,还是我需要重新考虑我的方法?不确定我是否会反序列化此数据,因为它在执行此代码之前已经反序列化。这就是为什么它在数据表中。所以我想我可以绑定。只是不确定我要绑定什么。

向下到代码的底部,就在最后一个 else 语句是我试图序列化的地方之前,然后我意识到我无法序列化,因为这些代码都不能用于此。

代码

private void ToolPull_Click(object sender, EventArgs e)
    {
        if (transactionSerial_Box.Text.Length == 4)
        {
            //define variables
            double cost = Convert.ToDouble(active_Cost.Text);
            int serial = Convert.ToInt32(transactionSerial_Box.Text);
            string employee = Convert.ToString(transactionEmployee_Box.Text);
            string description = Convert.ToString(active_Description.Text);
            string vendor = Convert.ToString(active_Vendor.Text);
            DateTime timeNow = DateTime.Now;

            //json file holding all data to be parsed.
            string myDynamicJSON = File.ReadAllText(@"transactionlog.json");


            //deserialize object containing all entries
            var logs = JsonConvert.DeserializeObject<List<TransactionLogEntry>>(myDynamicJSON) ?? new List<TransactionLogEntry>();


            //append current transaction to object
            logs.Add(new TransactionLogEntry()
            {
                Employee = employee,
                CurrentDate = timeNow,
                Serial = serial,
                Vendor = vendor,
                Description = description,
                IsPull = true,
                Cost = cost

            });

            //serialize the object back to file for transaction log
            myDynamicJSON = JsonConvert.SerializeObject(logs, Formatting.Indented);
            System.IO.File.WriteAllText(@"transactionlog.json", myDynamicJSON);

            //tool data starts here


            int i = 0; //counter

            Object onHand = tooldataSet.Tool.Rows[i]["OnHand"]; // reads value from datatable 
            onhand column
            Object stock = tooldataSet.Tool.Rows[i]["StockQty"];//reads value from datatable 
            stock column


            int newOnHand = (int)onHand - 1; //if user clicks pull then our onhand qty would 
            reduce by 1
            int order = (int)stock - (int)newOnHand; //updating the order column by doing math
            onHand = newOnHand; //sets orignal var to the updated value
            foreach (DataRow row in tooldataSet.Tool.Rows)
            {

                if (row["Serial #"].ToString() == transactionSerial_Box.Text)
                {

                    if (newOnHand >= 0)
                    {
                        row.SetField("OnHand", onHand);
                        row.SetField("OrderQty", order);
                    }
                    else
                    {
                        row.SetField("OnHand", 0);
                    }
                }
                i++;
            }

            //clear the input boxes after each entry.
            transactionEmployee_Box.Text = "";
            transactionSerial_Box.Text = "";

            //serialize the object back to file
            myDynamicJSON = JsonConvert.SerializeObject(ToolJson, Formatting.Indented);
            System.IO.File.WriteAllText(@"testLibrary.json", myDynamicJSON);
        }
        else
        {
            MessageBox.Show("Entry is not a valid serial number. Please try again.", "invalid serial number", MessageBoxButtons.OK, MessageBoxIcon.Error);
            transactionSerial_Box.Text = "";
        }

    }

标签: c#json

解决方案


推荐阅读