ComponentOne GridView for ASP.NET WebForms
In This Topic
    GridView to Excel
    In This Topic

    You can export the C1GridView data to Excel by implementing the following steps:

    In Designer:

    Complete the following steps:

    1. Right-click the C1GridView control and then bind it to the C1Nwind.mdb database. For detailed steps go to Step 1 of 3: Binding C1GridView to a DataSource.
    2. Select the AutogenerateColumns property to True
    3. Add a button to the form and name it as "Export".

    In Source View:

    1. To bind C1GridView with Customers table of C1Nwind.mdb database, modify the <cc1:C1GridView ></cc1:C1GridView > tag as shown below:

      <cc1:C1GridView ID="C1GridView1" runat="server" AutogenerateColumns="True" DataKeyNames="CustomerID" DataSourceID="SqlDataSource1">
                  <Columns>
                      <cc1:C1BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID">
                      </cc1:C1BoundField>
                      <cc1:C1BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName">
                      </cc1:C1BoundField>
                      <cc1:C1BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName">
                      </cc1:C1BoundField>
                      <cc1:C1BoundField DataField="ContactTitle" HeaderText="ContactTitle" SortExpression="ContactTitle">
                      </cc1:C1BoundField>
                      <cc1:C1BoundField DataField="Country" HeaderText="Country" SortExpression="Country">
                      </cc1:C1BoundField>
                      <cc1:C1BoundField DataField="City" HeaderText="City" SortExpression="City">
                      </cc1:C1BoundField>
                  </Columns>
              </cc1:C1GridView>
    2. Add a button to perform the excel export operation:

       <asp:Button ID="Button1" runat="server"  Text="Export" OnClick="Button1_Click" />

    In Code:

    1. Web controls have a RenderControl() method which outputs the server control content to a provided HtmlTextWriter object. The tracing information, about the control, is stored in the HtmlTextWriter object if tracing is enabled. This HtmlTextWriter is then output to a StringWriter object.

      Use the following code to save the C1GridView into an Html string:

      To write the code in Visual Basic

      Visual Basic
      Copy Code
       Public Function DataGridToExcel(ByVal dgExport As C1.Web.Wijmo.Controls.C1GridView.C1GridView) As String
              'create a string writers
              Dim stringWrite As New System.IO.StringWriter()
              'create an htmltextwriter which uses the stringwriter
              Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)

              Dim dg As C1.Web.Wijmo.Controls.C1GridView.C1GridView
              'just set the input datagrid = to the new dg grid
              dg = dgExport
              'Make the header text bold
              dg.HeaderStyle.Font.Bold = True
              'If needed, here's how to change colors/formatting at the component level
              dg.HeaderStyle.ForeColor = System.Drawing.Color.Black
              dg.RowStyle.ForeColor = System.Drawing.Color.Black
              'bind the modified datagrid
              'tell the datagrid to render itself to our htmltextwriter
              dg.AllowSorting = False
              dg.AllowPaging = False
              dg.AllowCustomPaging = False
              'new code
              Dim parent As Control = dg.Parent
              parent.Controls.Remove(dg)
              dg.RenderControl(htmlWrite)
              'new code
              parent.Controls.Add(dg)

              'output the html
              Return stringWrite.ToString()
          End Function

      To write the code in C#

      C#
      Copy Code
      public string DataGridToExcel(C1.Web.Wijmo.Controls.C1GridView.C1GridView dgExport)
              {
                  //create a string writers
                  System.IO.StringWriter stringWrite = new System.IO.StringWriter();
                  //create an htmltextwriter which uses the stringwriter
                  System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
                  C1.Web.Wijmo.Controls.C1GridView.C1GridView dg = default(C1.Web.Wijmo.Controls.C1GridView.C1GridView);
                  //just set the input datagrid = to the new dg grid
                  dg = dgExport;
                  //Make the header text bold
                  dg.HeaderStyle.Font.Bold = true;>
                  //If needed, here's how to change colors/formatting at the component level
                  dg.HeaderStyle.ForeColor = System.Drawing.Color.Black;
                  dg.RowStyle.ForeColor = System.Drawing.Color.Black;
                  //bind the modified datagrid
                  //tell the datagrid to render itself to our htmltextwriter

                  dg.AllowSorting = false;
                  dg.AllowPaging = false;
                  dg.AllowCustomPaging = false;
                 //new code
                  Control parent = dg.Parent;
                  parent.Controls.Remove(dg);>
                  dg.RenderControl(htmlWrite);
                 //new code
                  parent.Controls.Add(dg);
                 //output the html
                  return stringWrite.ToString();
              }
    2. Use the following code to call DownloadToExcel method:

      To write the code in Visual Basic

      Visual Basic
      Copy Code
      Public Sub DownloadToExcel(ByVal content As String, ByVal response As HttpResponse)
              'clean up the response.object
              response.Clear()
              response.Buffer = True
              response.Charset = ""
              'set the response mime type for excel
              response.ContentType = "application/vnd.ms-excel"
              response.ContentEncoding = New System.Text.UTF8Encoding()
              response.Write(content)
              response.End()
          End Sub

      To write the code in C#

      C#
      Copy Code
       public void DownloadToExcel(string content, HttpResponse response)
              {
                  //clean up the response.object
                  response.Clear();
                  response.Buffer = true;
                  response.Charset = "";
                  //set the response mime type for excel
                  response.ContentType = "application/vnd.ms-excel";
                  response.ContentEncoding = new System.Text.UTF8Encoding();
                  response.Write(content);
                  response.End();
              }
    3. Use the following code to handle the Button1_Click event:

      To write the code in Visual Basic

      Visual Basic
      Copy Code
      Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
              DownloadToExcel(DataGridToExcel(Me.C1GridView1), Response)
          End Sub

      To write the code in C#

      C#
      Copy Code
       protected void Button1_Click(object sender, EventArgs e)
              {
                  DownloadToExcel(DataGridToExcel(this.C1GridView1), Response);
              }

    Tip: If you face "RegisterForEventValidation can only be called during Render();" error message, try the workaround given in our online blog.

    The above error is also noticed when C1GridView contains a checkbox or a template column. This issue, being present in MS Gridview also, is a limitation of the C1GridView control as it is inherited from MS GridView.