c# - 我需要序列化它以保存到文件吗?
问题描述
我有一个从 .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# - OPC UA 订阅在快速处理程序中获取 DisplayName
- css - 修复了可滚动 div 内的导航
- mongodb - 有没有办法限制在 M0ngoDB 中输入即将到来的日期
- javascript - 反应 MaterialUI 数据网格
- sql - SQL - 搜索多列
- java - Java OffsetDateTime 不同环境测试中的精度问题
- python - 将数据框列表转换为 3-D 数据框
- sql - 在 hive 中将年月转换为日期格式
- angular - 通过 Angular 10 中的 api 下载 Outlook 附件
- javascript - 将 js 应用货币转换器转换为 React