Export To A Text File From A DataGridView Control

Save Data From A DataGridView Control To A Text File

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

Double click on the form, and copy and the code below into the load event of the form.
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.


        private void Form1_Load(object sender, EventArgs e)
        {
            //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();
            }
        }





Realated Posts:

Display A Text File In A DataGridView
Write To A Text File
Dynamically Populate A DataGridView

Advertisements
About

Over 20 years programming experience 5 years Oracle SQL/Stored Procedure programming Over 10 years SQL 2000/2005/2008 Server SQL/Stored Procedure programming Over 18 years Visual Basic 3.0/4.0/5.0/6.0/.Net/2003/2005/2008/2010/2015 Over 10 years ASP.Net Over 7 years Visual C# Over 20 years Microsoft Office and VBA HTML JAVA COBOL EASYTRIEVE FORTRAN

Tagged with: , , , , , ,
Posted in Visual Studio Code Examples
2 comments on “Export To A Text File From A DataGridView Control
  1. Anunturi imobiliare…

    […]Export To A Text File From A DataGridView Control « Tech and Programming Tips[…]…

  2. anunturi, anunt, gratuit, bucuresti,…

    […]Export To A Text File From A DataGridView Control « Tech and Programming Tips[…]…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: