Write gridview data to a notepad C# (windows application)




Through this code I planned to show how create a text file(note pad) by the C# desktop application
and which is store in the own application running folder.

--------------------------
Name spaces need System.IO
//First of all you have to take the current application thread running path which will be the
//source path for the notepad

String appRunDir = System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath).ToString();

//Then get the new notepad which you need to create

String strDestinationFile;
strDestinationFile = appRunDir+"\\YourNotePadNameHere"+".txt";

//Then write the file to disk
  if (!File.Exists(strDestinationFile)) using (StreamWriter sw = File.CreateText(strDestinationFile)) { }

//Then write gridview data to the newly created note pad

TextWriter tw = new StreamWriter(strDestinationFile);

            for (int x = 0; x < GridView.Rows.Count - 1; x++)
            {
                for (int y = 0; y < GridView.Columns.Count; y++)
                {
                    tw.Write(GridView.Rows[x].Cells[y].Value.ToString());
                    if (y != GridView.Columns.Count - 1)
                    {
                        tw.Write("  ");
                    }
                }
                tw.WriteLine();
            }
            tw.Close();

            Process.Start("notepad.exe", strDestinationFile);

Comments

Popular Posts