将gridview写入文件不适用于嵌套for循环
问题描述:
我有这个循环,我正在使用它来获取gridview的所有行内的所有单元格的值,然后将其写入csv文件。我的循环如下所示:将gridview写入文件不适用于嵌套for循环
string filename = @"C:\Users\gurdip.sira\Documents\Visual Studio 2008\WebSites\Supressions\APP_DATA\surpressionstest.csv";
StreamWriter sWriter = new StreamWriter(filename);
string Str = string.Empty;
string headertext = "";
sWriter.WriteLine(headertext);
for (int i = 0; i <= (this.GridView3.Rows.Count - 1); i++)
{
for (int j = 0; j <= (this.GridView3.Columns.Count - 1); j++)
{
Str = this.GridView3.Rows[i].Cells[j].Text.ToString();
sWriter.Write(Str);
}
sWriter.WriteLine();
}
sWriter.Close();
这段代码的问题是,单步调试的时候,第二个循环(一个经历的列)不开始作为调试器不打这个循环,因此我文件是空的。
关于是什么导致了这个问题的任何想法?代码本身看起来很好。
感谢
答
我认为内环路应该访问细胞,不列:
for (int i = 0; i <= (this.GridView3.Rows.Count - 1); i++)
{
for (int j = 0; j <= (this.GridView3.Rows[i].Cells.Count - 1); j++)
{
Str = this.GridView3.Rows[i].Cells[j].Text.ToString();
sWriter.Write(Str);
}
}
答
中添加行号,如果我正确地理解你的问题,你的意思是说,当调试,你永远不会到达第8行。如果是这种情况,那么我希望你的问题是,当你实际上通过循环处理时,gridview中没有行。为了弄清楚为什么是这种情况,我会建议看看你的页面的后期周期
string filename = @"C:\Users\gurdip.sira\Documents\Visual Studio 2008\WebSites\Supressions\APP_DATA\surpressionstest.csv";//1
StreamWriter sWriter = new StreamWriter(filename);//2
string Str = string.Empty;//3
string headertext = ""; //4
sWriter.WriteLine(headertext); //5
for (int i = 0; i <= (this.GridView3.Rows.Count - 1); i++) //6
{ //7
for (int j = 0; j <= (this.GridView3.Columns.Count - 1); j++) //8
{ //9
Str = this.GridView3.Rows[i].Cells[j].Text.ToString();//10
sWriter.Write(Str);//11
}//12
sWriter.WriteLine();//13
}//14
sWriter.Close();//15
}//16
你已经验证你的网格确实包含行,是否正确? – Kilhoffer 2008-10-24 14:29:12