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

Create a HTML File From a DataGridView Control

First complete the following steps:


Create a new C# Windows Application in Visual Studio.
Add two button controls to the form.
Add a DataGridView Control to the form.

Double Click on the Form and add the code below to the Load Event of the Form

      private void frmDataGridExportHTML_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 the ‘button1’ Control and add the code below to the Click Event of the button.
This code displays the data from the DataGridView Control in a table format.

      private void button1_Click(object sender, EventArgs e)
      {
      string sLine = "< table border=" + "\"" + "1" + "\"" + ">
      ";
      //This line of code creates a html file for the data export.
      System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\HTMLFile1.html");
      file.WriteLine(sLine);
      sLine = "";
      try
      {
      sLine = "< tr >";
      file.WriteLine(sLine);
      //This for loop places the column headers into the first row of the HTML table.
      for (int c = 0; c <= dataGridView1.Columns.Count - 1; c++)
      {
      sLine = "< td >" + dataGridView1.Columns[c].HeaderText + "< /td >";
      file.WriteLine(sLine);
      }
      sLine = "";

      //This for loop loops through each row in the DataGridView.
      for (int r = 0; r <= dataGridView1.Rows.Count - 1; r++)
      {
      sLine = sLine + "< tr >";
      file.WriteLine(sLine);
      sLine = "";
      //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 = "< td >" + dataGridView1.Rows[r].Cells[c].Value + "";
      file.WriteLine(sLine);
      }
      //The exported text is written to the html file, one line at a time.
      sLine = "< /tr >";

      file.WriteLine(sLine);
      sLine = "";
      }
      sLine = "< /table >";

      file.WriteLine(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();
      }
      }
    

Double Click on the ‘button2’ Control and add the code below to the Click Event of the button.
This code displays the data in the DataGridView Control in a list format.

      private void button2_Click(object sender, EventArgs e)
      {
      string sLine = "";
      //This line of code creates a html file for the data export.
      System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\HTMLFile2.html");
      sLine = "";
      try
      {
      for (int r = 0; r <= dataGridView1.Rows.Count - 1; r++)
      {
      sLine = "";
      for (int c = 0; c <= dataGridView1.Columns.Count - 1; c++)
      {
      sLine = dataGridView1.Columns[c].HeaderText + "< br>";
      file.WriteLine(sLine);
      sLine = System.Convert.ToString(dataGridView1.Rows[r].Cells[c].Value) + "< br>";
      file.WriteLine(sLine);
      }
      sLine = "< p>";
      file.WriteLine(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