【发布时间】:2020-12-10 23:32:11
【问题描述】:
我有一个文本文件,其中存储有关生产信息和流程的信息。该过程由几个阶段组成,例如Stripping、Cleaning、Paint 等。这些阶段存储的日期可能是expected date、start date、finished date、delay date。
.txt 文件示例:
567,埃因霍温,21,Stripping_e=20/05/2020,Stripping_s=21/05/2020,Stripping_f=22/05/2020,Cleaning_e=23/05/2020,Cleaning_s=27/05/2020,Cleaning_f =28/05/2020,Paint_e=28/05/2020,Panint_s=28/05/2020,Paint_f=29/05/2020,Cabinet_e=29/05/2020,Cabinet_s=31/05/2020,Cabinet_f=, Table_e=,Table_s=,Table_f=,Stand_e=,Stand_s=,Stand_f=,Display_e=,Display_s=,Display_f=,UControls_e=,UControls_s=,UControls_f=,Test_e=,Test_s=,Test_f=,Prepack_e=,Prepack_s= ,Prepack_f=,Endpack_e=,Endpack_s=,Endpack_f=,Release_e=,Release_s=,Release_f=,
读完这个 .txt 文件后,我将它存储在 dataGridView 中,例如:
但是,我不知道如何将最新日期添加到相应的列中。如果我在文本文件中看到Stripping_f,我想覆盖Stripping 列中的Stripping_e 值。其余列也是如此。这也是我不一定有专栏的日期(预期、开始、完成或延迟)的原因。
这是我目前使用的代码:
private void IGT_Load(object sender, EventArgs e)
{
table.Columns.Add("SalesNr", typeof(int));
table.Columns.Add("PName", typeof(string));
table.Columns.Add("Type", typeof(int));
table.Columns.Add("Stripping", typeof(string));
table.Columns.Add("Cleaning", typeof(string));
table.Columns.Add("Paint", typeof(string));
table.Columns.Add("Cabinet", typeof(string));
table.Columns.Add("Table", typeof(string));
table.Columns.Add("Stand", typeof(string));
table.Columns.Add("Display", typeof(string));
table.Columns.Add("User Controls", typeof(string));
table.Columns.Add("Test", typeof(string));
table.Columns.Add("Pre-pack", typeof(string));
table.Columns.Add("End-pack", typeof(string));
table.Columns.Add("Release for Delivery", typeof(string));
dataGridView1.DataSource = table;
Import();
}
private void Import()
{
// get lines from the text file
string[] lines = File.ReadAllLines(@"..\..\567.txt");
string[] values;
for (int i = 0; i < lines.Length; i++)
{
values = lines[i].ToString().Split(',', '=');
string[] row = new string[values.Length];
for (int j = 0; j < values.Length; j++)
{
if(values[j] == "Stripping_e")
{
row[j] = values[j + 1].Trim();
j++;
}
else if (values[j] == "Stripping_s")
{
row[j-2] = values[j+1].Trim();
j++;
}
else if(values[j] == "Cleaning_s" || values[j] == "Cleaning_e" || values[j] == "Cleaning_f" || values[j] == "Cleaning_d")
{
row[j] = values[j + 1].Trim();
}
else
{
row[j] = values[j].Trim();
}
}
table.Rows.Add(row);
}
}
table 是一个DataTable 连接到dataGridView1 作为DataSource。
我以前从未使用过文本文件,因此非常感谢您的帮助!
【问题讨论】:
-
@sumit_programmer 我看到你回答了一个类似的问题,你能帮我解决这个问题吗?
-
您当前的代码有什么问题?你能更具体地描述一下吗?
-
@dymanoid 在我当前的代码中,我假设我已经预期、开始、完成和延迟。例如,根据这个假设,我使用
j-2进行剥离。但是,如果我没有stripping_s值,那么我的cleaning_e将永远无法正确找到,因为会执行j-3之类的操作... -
我需要查看更多的文本文件。看起来您有重复的文件部分,需要查看几个部分才能正确编写代码。
-
@jdweng 我编辑了这个例子。阶段不一定必须包含日期(这是因为我们在工厂还不知道日期),也可能会有延迟,例如
Cabinet_d=。
标签: c# datagridview text-files