【问题标题】:Parsing CSV into datagrid WinForms将 CSV 解析为数据网格 WinForms
【发布时间】:2022-01-10 10:56:54
【问题描述】:

我在这里的第一个问题。我让自己陷入了一些非常简单的事情。

我已成功读取 csv 并将其解析为数据网格。 我的问题是我有一个数据集,其中一列缺少数据。无法编辑数据集。

我的代码打开并读取然后解析 csv:

private void ContractsToolStripMenuItem_Click(object sender, EventArgs e)
    {
        DataTable table = new DataTable();
        table.Columns.Add("Vehicle ID", typeof(string));
        table.Columns.Add("Booked_date_year", typeof(string));
        table.Columns.Add("Booked_date_month", typeof(string));
        table.Columns.Add("Booked_date_day", typeof(string));
        table.Columns.Add("Booked_date_week_number", typeof(string));
        table.Columns.Add("Booked_date_day_of_month", typeof(string));
        table.Columns.Add("Number of Days Booked", typeof(string));
        table.Columns.Add("Booking Status", typeof(string));
        table.Columns.Add("Customer Name", typeof(string));
        table.Columns.Add("Customer Email", typeof(string));
        table.Columns.Add("Customer Telephone", typeof(string));

        dataGridView1.DataSource = table;

        string[] lines = File.ReadAllLines("*Hiding this filepath as it included my name*");
        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++)
            {
                row[j] = values[j].Trim();
            }
            table.Rows.Add(row);
        }
    }

我希望能够识别缺失的值,因此在填充我的数据网格时将缺失/替换为默认值。当前结果是客户电话缺少 Booked_date_day 应该是的值。我的目标不是仅仅将 Booked_date_day 识别为缺少值,而是主动检查是否有任何列应该缺少值。

我有一些想法:

  1. 我将每个值附加到一个数组中,并根据每个值的一些规则检查它,即 Booked_date_month 应该是 Jan、Feb 等月份之一。

  2. 不是真正的第二个想法(更像是最后的手段),只识别 Booked_date_day 在每一行都有一个空值。 (这是不好的做法,因此我想避免这种情况)

如果您能提供任何帮助,我将不胜感激。在提出问题时,如果有什么我可以做的,也请告诉我! :)

【问题讨论】:

    标签: c# winforms csv datagrid


    【解决方案1】:

    如果您想知道如何读取 CSV 文件并将 CSV 的内容填充到 Windows 窗体应用程序的 Datagridview 中,那么您来对地方了。 嗯,做事总是有困难的方法,但也有简单的方法。在本文中,我们将分享一种有效且简单的方法来读取 CSV 并以行和列的形式在 Datagridview 上显示内容。

    读取 CSV 文件

    using System.Linq;
    using System.Data;
    using Microsoft.VisualBasic.FileIO; // This namespace usage is important or else TextFieldParser method will lead to error
    using System.IO;
     
    namespace CSVApp
    {
        public class ReadCSV
        {
            public DataTable readCSV;
     
            public ReadCSV(string fileName, bool firstRowContainsFieldNames = true)
            {
                readCSV = GenerateDataTable(fileName, firstRowContainsFieldNames);
            }
     
            private static DataTable GenerateDataTable(string fileName, bool firstRowContainsFieldNames = true)
            {
                DataTable result = new DataTable();
     
                if (fileName == "")
                {
                    return result;
                }
     
                string delimiters = ",";
                string extension = Path.GetExtension(fileName);
     
                if (extension.ToLower() == "txt")
                    delimiters = "\t";
                else if (extension.ToLower() == "csv")
                    delimiters = ",";
     
                using (TextFieldParser tfp = new TextFieldParser(fileName))
                {
                    tfp.SetDelimiters(delimiters);
     
                    // Get The Column Names
                    if (!tfp.EndOfData)
                    {
                        string[] fields = tfp.ReadFields();
     
                        for (int i = 0; i < fields.Count(); i++)
                        {
                            if (firstRowContainsFieldNames)
                                result.Columns.Add(fields[i]);
                            else
                                result.Columns.Add("Col" + i);
                        }
     
                        // If first line is data then add it
                        if (!firstRowContainsFieldNames)
                            result.Rows.Add(fields);
                    }
     
                    // Get Remaining Rows from the CSV
                    while (!tfp.EndOfData)
                        result.Rows.Add(tfp.ReadFields());
                }
     
                return result;
            }
        }
    }
    
    

    Use from here

    【讨论】:

    • 唯一的问题是我必须使用这种方法来填充数据网格,因为我必须遵循一些约束,比如对列名进行硬编码,以便它们是不可变的。我的问题是识别其中一行中缺少值时
    • 这里是数据集的一个小sn-p。 """135,2019,July,27,1,0,Booked,Chuck Farrow,Chuck_Farrow296@dionrab.com,3-418-877-6084 71,2019,July,27,1,0,Booked,Aiden Nelson, Aiden_Nelson9739@bulaffy.com,1-503-278-8185 73,2019,July,27,1,0,Booked,Leslie Kent,Leslie_Kent3947@fuliss.net,1-430-235-3881"""
    • 其实我可以用这个方法。谢谢!
    猜你喜欢
    • 2021-05-01
    • 2023-03-23
    • 2014-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-21
    • 2015-10-08
    • 1970-01-01
    相关资源
    最近更新 更多