簡體   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