C# write to text file from gridview error

Hi - Im trying to write to a text file froma  gridview using the follwing code but get the error message at the bottom --

protected void Button2_Click(object sender, EventArgs e)
    {
        String strDestinationFile;
        strDestinationFile = "C:\\output.txt";
        TextWriter tw = new StreamWriter(strDestinationFile);

        //writing the header
        for (int x = 0; x < GridView1.Columns.Count; x++)
        {
            tw.Write(GridView1.Columns[x].HeaderText);
            if (x != GridView1.Columns.Count - 1)
            {
                tw.Write(", ");
            }

        }
        tw.WriteLine();

        //writing the data
        for (int x = 0; x < GridView1.Rows.Count - 1; x++)
        {
            for (int y = 0; y < GridView1.Columns.Count; y++)
            {
                tw.Write(GridView1.Rows[x].Cells.Value);
                if (y != GridView1.Columns.Count - 1)
                {
                    tw.Write(", ");
                }
            }
            tw.WriteLine();
        }
        tw.Close();



    }

ERROR MESSAGE
CS1061: 'System.Web.UI.WebControls.TableCellCollection' does not contain a definition for 'Value' and no extension method 'Value' accepting a first argument of type 'System.Web.UI.WebControls.TableCellCollection' could be found (are you missing a using directive or an assembly reference?)
owenjaAsked:
Who is Participating?
 
Obadiah ChristopherCommented:
Should be Text instead of Value

GridView1.Rows[x].Cells[y].Text;
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.