简体   繁体   English

如何在datagridview输出的列之间添加简单文本? C#

[英]How to add simple text between columns in datagridview output? C#

I am trying to convert datagridview to text file. 我正在尝试将datagridview转换为文本文件。 So when i am using this code i am getting that text output. 因此,当我使用此代码时,我会得到该文本输出。 Ex. 防爆。

"Start of the text! This 170 is number This 80 is number End of the text!" “文本的开头!这170是数字这80是数字文本的结尾!”

This 170 and 80 numbers are generating from datagridview. 这170和80个数字是从datagridview生成的。

I want to get that output. 我想获得该输出。 Ex. 防爆。

"Start of the text! This 170 is number 80 End of the text!" “文本的开头!这170是数字80的文本结尾!”

So i dont want to add "This " and " is number" on second column. 所以我不想在第二栏中添加“ This”和“ is number”。

Sorry for my bad English if this text is not understoodable. 如果这段文字无法理解,对不起,我的英语不好。

private void button3_Click(object sender, EventArgs e)
    {
        TextWriter writer = new StreamWriter(@"C:\Users\ESTIT09\Desktop\test.txt");
        for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
        {
            writer.WriteLine("Start of the text!");
            for (int j = 0; j < dataGridView1.Columns.Count; j++)
            {
                writer.Write("This " + dataGridView1.Rows[i].Cells[j].Value.ToString() + " is number" + "\r\n");
            }

            writer.WriteLine("End of the text!");
        }
        writer.WriteLine("end sub");
        writer.Close();
    }

This should work. 这应该工作。 Replace your for loop with this block. 将此块替换为for循环。

    for (int j = 0; j < dataGridView1.Columns.Count; j++)
                {
                    if(j==0) //change the index (0 or 1)for what you are looking for
                    {
                         writer.Write("This " + dataGridView1.Rows[i].Cells[j].Value.ToString() + " is number" + "\r\n");
                    }
                    else
                    {
                        writer.Write(dataGridView1.Rows[i].Cells[j].Value.ToString() + "\r\n");     
                    } 
                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM