【问题标题】:How not to print the last separator character in StreamWriter?如何不在 StreamWriter 中打印最后一个分隔符?
【发布时间】:2014-01-14 17:02:50
【问题描述】:

我想删除最后一个字符“|”从每一行。我不想用那个字符关闭最后一列。你能帮忙吗?

private void spremiUDatotekuToolStripMenuItem1_Click(object sender, EventArgs e)
{
    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
    {
        System.IO.StreamWriter sw = new
        System.IO.StreamWriter(saveFileDialog1.FileName);

        for (int x = 0; x < dataGridView1.Rows.Count - 1; x++)
        {

            for (int y = 0; y < dataGridView1.Columns.Count; y++)
            {
                sw.Write(dataGridView1.Rows[x].Cells[y].Value);
                if (y != dataGridView1.Columns.Count)
                {
                    sw.Write("|");                    
                }
            }
            sw.WriteLine();
        }
        sw.Close();
    }
}

【问题讨论】:

  • 不要这样做sw.Write("|");?我不确定你想在这里问什么。您应该仔细说明您的情况以及您要达到的目标。
  • 为了获得一些速度,我建议您从 DataSet 或 DataTable 中导出数据。除此之外,@rae1n 的解决方案很好。
  • 我相信 OP 想要打印 | 但仅限于元素内部,而不是行尾;换句话说,OP 想要1|2|3|4|5,而不是1|2|3|4|5|,这是当前正在打印的内容。
  • 感谢@rae1n 的澄清。

标签: c# datagridview character streamwriter


【解决方案1】:

改用dataGridView1.Columns.Count - 1

if (y != dataGridView1.Columns.Count - 1)
{
    sw.Write("|");                    
}

这样它就不会在该行的最后一个元素之后打印|,即y == dataGridView1.Columns.Count - 1

或者,就像@DaveZych 提到的那样,您可以使用string.Join 来避免检查迭代器并以这样的方式结束,

foreach (var row in dataGridView1.Rows)
{
    var rowValue = string.Join("|", row.Cells.Select(cell => cell.Value));
    sw.Write(rowValue);    
}

sw.Close();

【讨论】:

  • 我已经试过了。 Columns.Count -1 只保存最后一列,如果我想保存 5 列,现在只保存 4 列,带有“|”最后也是..
【解决方案2】:

您应该对照count - 1 对照count,而不是对照y

if (y != dataGridView1.Columns.Count - 1)
{
     sw.Write("|");                    
}

你从y &lt; count循环,这意味着y永远不会等于count

或者,您可以在整行上使用string.Join

for (int x = 0; x < dataGridView1.Rows.Count - 1; x++)
{
    string line = string.Join("|", dataGridView1.Rows[x].Cells.Select(c => c.Value);
    sw.WriteLine(line);
}

这将创建一个字符串,其中所有值都由| 分隔。

【讨论】:

  • 错误 1“System.Windows.Forms.DataGridViewCellCollection”不包含“Select”的定义,并且没有扩展方法“Select”接受“System.Windows.Forms.DataGridViewCellCollection”类型的第一个参数可以找到(您是否缺少 using 指令或程序集引用?)
  • 在文件顶部,为System.Linq 添加一个using 语句,例如using System.Linq;
【解决方案3】:

由于您使用了“

private void spremiUDatotekuToolStripMenuItem1_Click(object sender, EventArgs e)
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
    System.IO.StreamWriter sw = new
    System.IO.StreamWriter(saveFileDialog1.FileName);

    for (int x = 0; x < dataGridView1.Rows.Count; x++)
    {

        for (int y = 0; y < dataGridView1.Columns.Count; y++)
        {
            sw.Write(dataGridView1.Rows[x].Cells[y].Value);
            if (y != dataGridView1.Columns.Count - 1) // Count - 1 is the last value. y will never reach count because you have "<" sign
            {
                sw.Write("|");                    
            }
        }
        sw.WriteLine();
    }
    sw.Close();
}

}

【讨论】:

  • 如果我在数据网格中有 1 行,它会将 0 行数据保存到文本文件,因为这里需要第 -1 行。 Columns.Count -1 只保存最后一列,如果我想保存 5 列,现在只保存 4 列,使用 |最后也是..
  • 哪里需要第 1 行?您在哪里看到最后一列未保存在文件中? if 条件仅适用于分隔符。当 y 等于 count - 1 (这是列集合中的最后一个索引)时,您将不会打印 srparator。如果您有 1 行,则仅保存 rows(0)。
  • 对不起,但没有行数 -1 它会打印另一个不存在的行,带有“| | | |” .. 所以我用你的代码构建了解决方案,但将行数保持在 -1 并且它可以工作,谢谢!
  • 我明白,发生这种情况是因为你在最后一行有你显然不想打印的空列。很高兴你能够让它发挥作用;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多