Saturday, January 8, 2011

Csv File from the GridView

 StreamWriter sw = new StreamWriter("c:\\gridview.csv");

        for (int i = 0; i < GridView1.Columns.Count; i++)
        {
             sw.Write(GridView1.Columns[i].HeaderText);
            if (i != GridView1.Columns.Count)
            {
                 sw.Write(",");
            }
           
        }

        sw.Write(sw.NewLine);
       


            foreach (GridViewRow dr in GridView1.Rows)
            {
                 for (int i = 0; i < GridView1.Columns.Count; i++)
                 {
                     sw.Write(dr.Cells[i].Text);
                     if (i != GridView1.Columns.Count)
                     {
                          sw.Write(",");
                     }
        
                 }
                 sw.Write(sw.NewLine);
            }
            sw.Flush();
            sw.Close();

No comments:

Post a Comment