【问题标题】:Delete last line in txt file删除txt文件的最后一行
【发布时间】:2014-06-11 20:04:21
【问题描述】:

我为我的 datagridview 编写了一个 foreach 循环,以将所有行写入 txt 文件。 我有一个问题,因为它在末尾添加了 2 个空行,因为在添加/读取数据网格视图后 1 行充满了空单元格。 有什么办法可以在txt中省略最后一行或者删除?

foreach (DataGridViewRow item in this.DB.Rows)
{
    foreach (DataGridViewCell item2 in item.Cells)
    {   
        if(item2.Value != null)
        filewrite.Write(item2.Value.ToString() + " ");
    }
    filewrite.WriteLine("");
}

【问题讨论】:

    标签: c# datagridview


    【解决方案1】:

    您可以使用String.IsNullOrWhiteSpace 确保仅在单元格中有内容时才写入文件,例如

    var cellValue = item2.Value.ToString();
    if (!String.IsNullOrWhitespace(cellValue)) {
        filewrite.Write(cellValue + " ");
    }
    

    您实际上可以使用 LINQ 轻松做到这一点

    var rows = this.DB.Rows.Select(r => String.Join(" ", r.Cells.Select(c => c.Value)))
                           .Where(x => !String.IsNullOrWhiteSpace((string)x));
    
    File.WriteAllLines("database.txt", rows);
    

    【讨论】:

    • 不会这样工作。 item2.Value 不是字符串,并且 item2.Value.ToString() 也不起作用
    • @user3658127 我猜它不起作用,因为你还是执行了filewrite.WriteLine(""); 行。
    • @user3658127 更新为 ToString。如果您说检查不起作用,那显然是因为item2.Value 包含 某些东西。如果你的意思是你最后仍然有一个空行......是的,你会因为你在循环之后有filewrite.WriteLine("")。您的问题是“如何停止 2 空行” - 此代码将阻止空行输出,但不会阻止最后的空行,因为您似乎是故意这样做的。
    • 我在阅读方法中正确处理了1个空行,需要删除第二个,已经解决了:)我的问题不涉及倒数第二行,我按需使用它
    【解决方案2】:

    你可以像下面这样实现:

    for(int i = 0; i< this.DB.Rows.Count-1; i++)
    {
        foreach (DataGridViewCell item2 in item.Cells)
        {   
            if(item2.Value != null)
                filewrite.Write(item2.Value.ToString() + " ");
        }
    
        filewrite.WriteLine("");
    }
    

    【讨论】:

      【解决方案3】:

      如果您的数据中可能有其他空行。

      foreach (DataGridViewRow item in this.DB.Rows)
      {
          var printRow = false;
          foreach (DataGridViewCell item2 in item.Cells)
          {   
              if(!String.IsNullOrWhitespace(item2.Value.ToString()))
              {
                  printRow = true;
                  filewrite.Write(item2.Value.ToString() + " ");
              }
          }
          if (printRow)
          {
              filewrite.WriteLine("");
          }
      }
      

      【讨论】:

      • 不知道为什么它仍然说 item2.Value 是无效参数
      【解决方案4】:
      private void tofile_Click(object sender, EventArgs e)
      {
          string file = "database.txt";
          StreamWriter filewrite;
          filewrite = new StreamWriter(file);
          int rcount = DB.Rows.Count;
      
          for(int i = 0; i < this.DB.Rows.Count - 1; i++)
          {
              foreach (DataGridViewCell item2 in this.DB.Rows[i].Cells)
              {   
                  if(item2.Value != null)
                      filewrite.Write(item2.Value.ToString() + " ");
              }
      
              filewrite.WriteLine("");
          }
      
          filewrite.Close();
      }
      

      已解决:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多