简体   繁体   English

c#-将DataGridView转换为txt…同一行问题上的标题和第一行

[英]c# - DataGridView to txt…Header and first row on same line issue

I am attempting to export data from a datagridview into a .txt table of sorts. 我试图将数据从datagridview导出到各种.txt表中。

When I export to the .txt, the header prints on the first line like it is supposed too, but the first row of the table prints on that line too...Now I have tried a number of things but, obviously my logic is very wrong here. 当我导出到.txt文件时,标头也应该在第一行上打印,但是表的第一行也在该行上打印...现在我已经尝试了很多方法,但是显然我的逻辑是这里非常错误。

I basically want a way to set dataGridView_Excel.Rows.Count - 1; 我基本上想要一种方法来设置dataGridView_Excel.Rows.Count-1; to just the normal count without the -1. 到没有-1的正常计数。 But every time I do it throws a Object reference not set to an instance of an object. 但是每次我这样做时,都会抛出一个未设置为对象实例的Object引用。 Or if I try to increase the column count to counteract that I get a Index was out of range. 或者,如果我尝试增加列数以抵消我得到的索引超出范围。 Must be non-negative and less than the size of the collection. 必须为非负数并且小于集合的大小。 Any suggestions on how to do this better? 关于如何做得更好的任何建议? I understand why its happening, my logic to fix it just isn't sound and I am beyond confused. 我了解为什么会发生这种情况,但我解决该问题的逻辑并不健全,而且我很困惑。

if (save.ShowDialog() == DialogResult.OK)
        {
            TextWriter writer = new StreamWriter(save.OpenFile());

            int i2 = 0;
            while (i2 < 19)
            {
                writer.Write(dataGridView_Excel.Columns[i2].HeaderText + "\t        " + "|");
                i2++;
            }

            for (int i = 0; i < dataGridView_Excel.Rows.Count - 1; i++)
            {                                      
                for (int j = 0; j < dataGridView_Excel.Columns.Count; j++)
                {
                    writer.Write("\t" + dataGridView_Excel.Rows[i].Cells[j].Value.ToString() + "\t        " + "|");

                }

                writer.WriteLine("");
                writer.WriteLine("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
            }
            writer.Close();
            MessageBox.Show("Data Exported");

Your DataGridView is 0-indexed and has 'count' total rows. 您的DataGridView索引为0,并且具有“总”行数。 This line: for (int i = 0; i < dataGridView_Excel.Rows.Count - 1; i++) 这行代码:for(int i = 0; i <dataGridView_Excel.Rows.Count-1; i ++)

Will start at zero and go to the ((numbers of rows -1)-1), because it is < : Less than. 将从零开始,然后转到((行数-1)-1),因为它<小于。

Try: 尝试:

for (int i = 0; i <= dataGridView_Excel.Rows.Count - 1; i++)

ie add in the = sign, or: 即添加=符号,或:

for (int i = 0; i < dataGridView_Excel.Rows.Count; i++)

Unless I've misunderstood your issue. 除非我误解了您的问题。 Is the issue the looping, or is it getting things to write on a new line? 问题是循环出现,还是让事情写在新的一行上?

To get it to write a new line, try adding Environment.NewLine to the end: 要使其编写新行,请尝试在末尾添加Environment.NewLine:

writer.Write("\t" + dataGridView_Excel.Rows[i].Cells[j].Value.ToString() + "\t        " + "|" + Environment.NewLine);

Just a case of looking at something too long and getting distracted...oops 只是看东西太长而分心的情况...糟糕

Here is the easiest way around this: 这是解决此问题的最简单方法:

if (save.ShowDialog() == DialogResult.OK)
        {
            TextWriter writer = new StreamWriter(save.OpenFile());

            int i2 = 0;
            while (i2 < 20)
            {
                writer.Write(dataGridView_Excel.Columns[i2].HeaderText + "\t        " + "|");
                i2++;
            }

            writer.Write(Environment.NewLine);
            writer.WriteLine("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");

            for (int i = 0; i < dataGridView_Excel.Rows.Count - 1; i++)
            {                                      
                for (int j = 0; j < dataGridView_Excel.Columns.Count; j++)
                {
                    writer.Write("\t" + dataGridView_Excel.Rows[i].Cells[j].Value.ToString() + "\t        " + "|");

                }

                writer.WriteLine("");
                writer.WriteLine("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
            }
            writer.Close();
            MessageBox.Show("Data Exported");
        }

I just added a Environment.NewLine and my ---- divider before the loop. 我只是在循环之前添加了Environment.NewLine和----分隔符。 The whole reason I added the Header to the file in a while loop outside the main for loop was for easy changes down the road to the .txt format. 我在主for循环之外的while循环中将Header添加到文件的全部原因是为了轻松地将其更改为.txt格式。 Thank you for the idea @ainwood it set me back on track! 谢谢@ainwood的想法,它使我重回正轨!

Before 之前

After! 后!

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

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