【问题标题】:C# datagridview didnt show full data from text file after merge two text file together将两个文本文件合并在一起后,C# datagridview 没有显示来自文本文件的完整数据
【发布时间】:2018-09-14 20:25:09
【问题描述】:

在我将两个文本文件合并在一起并尝试在我的 datagridview 中读取合并后的文件后,我意识到我的 datagridview 没有显示我的文本文件的完整数据。

这是我调试后的datagridview。enter image description here

这是我合并后的文本文件格式。(我不能按顺序合并文件,因为我还是编码新手) enter image description here

这是我的代码。

 private bool loadTxt()
    {
        // try locate the txt file, exist or not
        try
        {
            StreamReader rrr = new StreamReader("merge.txt");
            rrr.Close();
        }
        catch (FileNotFoundException ex)
        {
            return false;
        }

        // read all line from txt file
        var file = File.ReadAllLines("merge.txt");
        string DateTag = "<Date>";
        string NIDTag = "<News ID>";
        string StockIDTag = "<StockID>";
        string LatestPriceTag = "<Latest Price>";
        string TargetPriceTag = "<Target Price>";
        string StockComTag = "<StockCompany>";

        string Date = "";
        string NID = "";
        string StockID = "";
        string LatestPrice = "";
        string TargetPrice = "";
        string StockCom = "";

        int row = dataGridView2.Rows.Count - 1;

        // Loop through the file and find targeted data to store in datagridview1
        for (int i = 0; i < file.Length - 1; i++)
        {
            if (file[i] == DateTag)
            {
                dataGridView2.Rows.Add();
                Date = file[i + 1];
                dataGridView2.Rows[row].Cells[0].Value = Date;
            }

            if (file[i] == NIDTag)
            {
                NID = file[i + 1];
                dataGridView2.Rows[row].Cells[1].Value = NID;
            }

            if (file[i] == StockIDTag)
            {
                StockID = file[i + 1];
                dataGridView2.Rows[row].Cells[2].Value = StockID;
            }

            if (file[i] == LatestPriceTag)
            {
                LatestPrice = file[i + 1];
                dataGridView2.Rows[row].Cells[3].Value = LatestPrice;
            }

            if (file[i] == TargetPriceTag)
            {
                TargetPrice = file[i + 1];
                dataGridView2.Rows[row].Cells[4].Value = TargetPrice;       
            }

            if (file[i] == StockComTag)
            {
                StockCom = file[i + 1];
                dataGridView2.Rows[row].Cells[5].Value = StockCom;
                row++;
            }
        }

        return true;
    }

谢谢。

【问题讨论】:

    标签: c#


    【解决方案1】:

    我认为您正在将数据添加到同一行

    int row = dataGridView2.Rows.Count - 1;  //your row 
    

    并使用row 将每个值添加到dataGridView2

    if (file[i] == DateTag)
                {
                    dataGridView2.Rows.Add();
                    Date = file[i + 1];
                    dataGridView2.Rows[row].Cells[0].Value = Date; //same [row]
                }
    

    希望对你有帮助!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-02
      • 1970-01-01
      • 1970-01-01
      • 2017-01-29
      • 2021-11-08
      相关资源
      最近更新 更多