C# 如何不打印StreamWriter中的最后一个分隔符?

C# 如何不打印StreamWriter中的最后一个分隔符?,c#,datagridview,character,streamwriter,C#,Datagridview,Character,Streamwriter,我想从每行中删除最后一个字符“|”。我不想让最后一列用那个字符结束。你能帮忙吗 private void spremiUDatotekuToolStripMenuItem1_Click(object sender, EventArgs e) { if (saveFileDialog1.ShowDialog() == DialogResult.OK) { System.IO.StreamWriter sw = new System.IO.Stream

我想从每行中删除最后一个字符“|”。我不想让最后一列用那个字符结束。你能帮忙吗

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();
    }
}
private void spremiUDatotekuToolStripMenuItem1\u单击(对象发送方,事件参数e)
{
if(saveFileDialog1.ShowDialog()==DialogResult.OK)
{
System.IO.StreamWriter sw=新
System.IO.StreamWriter(saveFileDialog1.FileName);
对于(int x=0;x
试试
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();

请尝试使用
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();

请尝试使用
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();

请尝试使用
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();

您不应该根据
计数检查
y
,而应该根据
计数-1检查

if (y != dataGridView1.Columns.Count - 1)
{
     sw.Write("|");                    
}
您正在从
y
循环,这意味着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);
}
for(int x=0;xc.Value);
西南写入线(行);
}

这将创建一个字符串,其中所有值由

分隔,而不是根据
计数检查
y
,而是根据
计数-1检查

if (y != dataGridView1.Columns.Count - 1)
{
     sw.Write("|");                    
}
您正在从
y
循环,这意味着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);
}
for(int x=0;xc.Value);
西南写入线(行);
}

这将创建一个字符串,其中所有值由

分隔,而不是根据
计数检查
y
,而是根据
计数-1检查

if (y != dataGridView1.Columns.Count - 1)
{
     sw.Write("|");                    
}
您正在从
y
循环,这意味着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);
}
for(int x=0;xc.Value);
西南写入线(行);
}

这将创建一个字符串,其中所有值由

分隔,而不是根据
计数检查
y
,而是根据
计数-1检查

if (y != dataGridView1.Columns.Count - 1)
{
     sw.Write("|");                    
}
您正在从
y
循环,这意味着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);
}
for(int x=0;xc.Value);
西南写入线(行);
}

这将创建一个字符串,其中所有值均由
|

分隔。您将丢失代码的最后一行,因为您使用“您将丢失代码的最后一行,因为您使用”您将丢失代码的最后一行,因为您使用“您将丢失代码的最后一行,因为您使用”不要这样做
sw.Write(“|”)
?我不确定你想问什么。你应该仔细说明你的情况以及你想要达到的目标。为了提高速度,我建议你从数据集或数据表中导出数据。除此之外,@rae1n的解决方案很好。我相信OP想要打印
|
,但只用于内部e元素,不在行的末尾;换句话说,OP想要
1 | 2 | 3 | 4 | 5
,而不是
1 | 2 | 3 | 4 | 5
,这是当前正在打印的内容。感谢您澄清@rae1n。不要这样做
sw.Write(“|”)
?我不确定你想问什么。你应该仔细说明你的情况以及你想要达到的目标。为了提高速度,我建议你从数据集或数据表中导出数据。除此之外,@rae1n的解决方案很好。我相信OP想要打印
|
,但只用于内部e元素,而不是在行的末尾;换句话说,OP想要的是当前正在打印的
1 | 2 | 3 | 4 | 5
,而不是
1 | 2 | 3 | 4 | 5
。谢谢