如何从.NET DataGridView控件的单元格值写入文本文件? [英] How to write a text file from a .NET DataGridView control cell values?
本文介绍了如何从.NET DataGridView控件的单元格值写入文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面的代码应该循环遍历 DataGridView
中的所有行,并将所有单元格值写入文本文件中。
但是,它会输出所有的行,但只输出每个行的第一个单元格,而不是其他三个单元格。
I have the following code that should loop through all the rows in my DataGridView
, and write all their cell values to a text file.
However, it outputs all the rows, but only the first cell of each one, and not the other three cells.
string file_name = "C:\\test1.txt";
var objWriter = new System.IO.StreamWriter(file_name);
int count = dgv.Rows.Count;
for (int row = 0; row < count; row++)
{
objWriter.WriteLine(dgv.Rows[row].Cells[0].Value.ToString());
}
objWriter.Close();
推荐答案
for (int row = 0; row < count; row++)
{
int colCount = dgv.Rows[row].Cells.Count;
for ( int col = 0; col < colCount; col++)
{
objWriter.WriteLine(dgv.Rows[row].Cells[col].Value.ToString());
}
// record seperator could be written here.
}
虽然使用foreach循环会更干净。
Although, it would be cleaner if you used a foreach loop.
这篇关于如何从.NET DataGridView控件的单元格值写入文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文