Greg Owen
Over 27 Years of Programming Experience
Programming Examples, Portfolio, and More
Home

Export To A Text File From A DataGridView Control

Steps:
Create a new C# project.
Add a button control to Form1.
Add a DataGridView Control to Form1.



      
      private void Form1_Load(object sender, EventArgs e)
      {
      //Double click on the form, and copy and paste the code below into the load event of the form.[-br-][/br-]
      //The code below is optional. The purpose is to put data into the DataGridView Control. If you already have
      data in the form, you will not need to use the code below.

      //If you manually add rows to a DataGridView, you must disable the
      //AllowUserToAddRows function.  The function can be enabled after
      //you have added the rows.
      dataGridView1.AllowUserToAddRows = false;

      //The code below adds Columns to the DataGridView control

      DataGridViewColumn colHold = new DataGridViewTextBoxColumn();
      colHold.Name = "col1";
      colHold.HeaderText = "FIELD1";
      dataGridView1.Columns.Add(colHold);

      colHold = new DataGridViewTextBoxColumn();

      colHold.Name = "col2";
      colHold.HeaderText = "FIELD2";
      dataGridView1.Columns.Add(colHold);

      colHold = new DataGridViewTextBoxColumn();

      colHold.Name = "col3";
      colHold.HeaderText = "FIELD3";
      dataGridView1.Columns.Add(colHold);

      colHold = new DataGridViewTextBoxColumn();

      colHold.Name = "col4";
      colHold.HeaderText = "FIELD4";
      dataGridView1.Columns.Add(colHold);

      //The code below adds rows and fills cells with values to be exported.

      dataGridView1.Rows.Add();

      dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0].Value = "1";
      dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[1].Value = "2";
      dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[2].Value = "3";
      dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[3].Value = "4";

      dataGridView1.Rows.Add();

      dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0].Value = "5";
      dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[1].Value = "6";
      dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[2].Value = "7";
      dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[3].Value = "8";

      }


      Double click on button1 on the form and copy and paste the code below into the click event on the form:

      private void button1_Click(object sender, EventArgs e)
      {
      //This line of code creates a text file for the data export.

      System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\TextFile.txt           try
      {
      string sLine = "";

      //This for loop loops through each row in the table
      for (int r = 0; r <= dataGridView1.Rows.Count - 1; r++)
      {
      //This for loop loops through each column, and the row number
      //is passed from the for loop above.
      for (int c = 0; c <= dataGridView1.Columns.Count - 1; c++)
      {
      sLine = sLine + dataGridView1.Rows[r].Cells[c].Value;
      if (c != dataGridView1.Columns.Count - 1)
      {
      //A comma is added as a text delimiter in order
      //to separate each field in the text file.
      //You can choose another character as a delimiter.
      sLine = sLine + ",";
      }
      }
      //The exported text is written to the text file, one line at a time.
      file.WriteLine(sLine);
      sLine = "";
      }

      file.Close();
      System.Windows.Forms.MessageBox.Show("Export Complete.", "Program Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
      }
      catch (System.Exception err)
      {
      System.Windows.Forms.MessageBox.Show(err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
      file.Close();
      }
      }
      
    
#CSharp #VisualStudio
Some words from the sponsors...

Add a C# Dropdown to a Bootstrap Modal Dialog

This example combines C# and Bootstrap to create a bootstrap modal dialog containing an asp dropdown and an asp button to retrieve...

Continue Reading

Use C# to Populate Dropdown Lists With XML

The code below opens an xml file, reads the data from the xml file, dynamically adds a label for the dropdown list, and adds the dropdown list to the asp.net page...

Continue Reading

Add Style Properties to ASP.Net Controls to Improve Appearance

The default formatting for ASP.Net controls is very dated.  The default formatting is basically the same as the formatting in Visual Basic 1.0 ...

Continue Reading

Export To A Text File From A DataGridView Control

Add a button control to Form1.
Add a DataGridView Control to Form1....

Continue Reading