繁体   English   中英

将 CSV 解析为 datagrid WinForms

[英]Parsing CSV into datagrid WinForms

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

我已成功读取 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. 我 append 将每个值放入一个数组中,并根据每个值的一些规则检查它,即 Booked_date_month 应该是一月、二月等月份之一。

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

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

如果您想知道如何阅读 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;
        }
    }
}

从这里使用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM