我需要帮助将两个不同的行(具有相同的列)添加到同一个数据表中

使用从 csv 文件中读取数据并将数据添加到 Datagridview 的 Windows 窗体应用程序。我遇到了将所有行添加到数据表并显示在数据网格视图上的问题。datagridview 仅显示前两个 if 条件和 OneRow if 条件的数据行。如果 dataable 和 datagridview 行填充了 OneRow if 条件行,它将不会添加来自 twoRow if 条件的行。但我希望显示 OneRow 和 TwoRow 中的行。当我在 OneRow if 条件中注释(/**/)时,来自 TwoRow 的行也会填充数据表和 datagridview。但我需要两者来填充表格。提前致谢!


        Construct.MainDataTable.Columns.Add("Date", typeof(DateTime));

        Construct.MainDataTable.Columns.Add("Time");

        Construct.MainDataTable.Columns.Add("Serial");

        Construct.MainDataTable.Columns.Add("Type");

        Construct.MainDataTable.Columns.Add("level");

        Construct.MainDataTable.Columns.Add("price");

        Construct.MainDataTable.Columns.Add(" Limit");

        Construct.MainDataTable.Columns.Add("last Limit");

        Construct.MainDataTable.Columns.Add("Data");

        ..........................

    ...............................................

        DataRow oneRow = Construct.MainDataTable.NewRow();

        DataRow twoRow = Construct.MainDataTable.NewRow();

        dataGridView2.AllowUserToAddRows = false;


        if (line.Split(',')[2].Equals("Time"))

        {

         time = line.Split(',')[3];

         date = line.Split(',')[1];

         }

        if (line.Split(',')[2].Equals("Level"))

        {

         level = line.Split(',')[3];


         }

        //OneROw(IF condition)

        if ((Convert.ToDecimal(line.Split(',')[8])) < (Convert.ToDecimal     (line.Split(',')[12]))) 

        {


          type = line.Split(',')[1];

          serial = line.Split(',')[7];

          price = line.Split(',')[3];

          Limit = line.Split(',')[8];

          lastLimit = line.Split(',')[10];

          Data = line.Split(',')[12];

         }  

     }


PIPIONE
浏览 79回答 1
1回答

慕森卡

无法添加评论,因为我没有足够的业力,所以我在这里问我的问题:所以,如果我理解你的问题,你不能从一个 .csv 文件中添加数据,如果它有超过一行?为什么对 .csv 文件中的行使用 2 种不同的 if 条件?如果行中有空数据,请不要介意,您仍然可以将它们放置到 DataTable 列中,因此您可以使用循环将 .csv 中的数据添加到 DataTable 中。尝试这样的事情:&nbsp; &nbsp; public static DataTable CsvToDataTable(string csv)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; DataTable dt = new DataTable();&nbsp; &nbsp; &nbsp; &nbsp; string[] lines = csv.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);&nbsp; &nbsp; &nbsp; &nbsp; Regex onlyDeimiterComma = new Regex(",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))");&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < lines.Length; i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataRow row = dt.NewRow();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string[] cells = onlyDeimiterComma.Split(lines[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < cells.Length; j++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i == 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (j == 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Columns.Add(cells[j], typeof(DateTime));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Columns.Add(cells[j]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; row[j] = cells[j];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Rows.Add(row);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return dt;&nbsp; &nbsp; }只需在代码中的任何位置调用此方法并从 .csv 文件中读取字符串即可。您可以尝试在此处编译此代码,并查看它如何处理具有不同数据(空列、引用文本、引用逗号)的 .csv 数据UPD:如果您需要从两个不同的 .csv 文件填充 DataTable,您仍然可以使用上面的代码。只需为两个文件调用两次,然后合并两个 DataTable,如下所示:DataTable dt = CsvToDataTable(csvFileOne);DataTable dtTwo = CsvToDataTable(csvFileTwo);dt.Merge(dtTwo);
打开App,查看更多内容
随时随地看视频慕课网APP