In this article I will explain with an example, how to export multiple GridViews to Word document in ASP.Net using C# and VB.Net.
 
 
Database
Here I am making use of the Microsoft’s Northwind Database. You can download it from here.
 
 
HTML Markup
The following HTML Markup consists of:
GridView – For displaying Customers data and Order details.
Columns
Both GridView consist of two BoundField columns.
 
Properties
AllowPaging – For enabling paging in the GridView control. Here it is set to true.
Events
Both GridView have been assigned with an OnPageIndexChanging event handler.
 
RadioButtonList:-
Two RadioButtonList controls will be used for getting following Information:
Paging will be enabled or not means export current page or export all pages of GridView.
Preference of export i.e. Vertical or Horizontal.
 
Button – For exporting GridView data to Word format.
The Button has been assigned with an OnClick event handler.
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" AllowPaging="true" OnPageIndexChanging="OnPaging">
    <Columns>
        <asp:BoundField ItemStyle-Width="150px" DataField="CustomerID" HeaderText="Customer Id" />
        <asp:BoundField ItemStyle-Width="150px" DataField="City" HeaderText="City" />
    </Columns>
</asp:GridView>
<br />
<br />
<asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="false" AllowPaging="true" OnPageIndexChanging="OnPaging">
    <Columns>
        <asp:BoundField ItemStyle-Width="150px" DataField="OrderID" HeaderText="Order Id" />
        <asp:BoundField ItemStyle-Width="150px" DataField="CustomerID" HeaderText="Customer Id" />
    </Columns>
</asp:GridView>
<br />
Paging Enabled?
<asp:RadioButtonList ID="rbPaging" runat="server">
    <asp:ListItem Text="Yes" Value="True" Selected="True"></asp:ListItem>
    <asp:ListItem Text="No" Value="False"></asp:ListItem>
</asp:RadioButtonList>
<br/>
Export Preference:
<br />
<br />
<asp:RadioButtonList ID="rbPreference" runat="server">
    <asp:ListItem Text="Vertical" Value="1" Selected="True"></asp:ListItem>
    <asp:ListItem Text="Horizontal" Value="2"></asp:ListItem>
</asp:RadioButtonList>
<br />
<asp:Button ID="btnExportWord" runat="server" Text="Export To Word" OnClick="ExportToWord" />
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
 
VB.Net
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
 
 
Binding Multiple GridViews
Inside the Page_Load event handler, the GetData method is called which accepts SQL query as a parameter.
Inside the GetData method, using SQL query data is fetched from the Northwind database and stored in an object of the DataTable.
Finally, the object of the DataTable is returned.
C#
protected void Page_Load(object sender, EventArgs e)
{
    gvCustomers.DataSource = this.GetData("SELECT CustomerID, City FROM Customers");
    gvCustomers.DataBind();
 
    gvOrders.DataSource = this.GetData("SELECT OrderID, CustomerID FROM Orders");
    gvOrders.DataBind();
}
 
private DataTable GetData(string query)
{
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter(query, con))
        {
            using (DataTable dt = new DataTable())
            {
                sda.Fill(dt);
                return dt;
            }
        }
    }
 
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    gvCustomers.DataSource = Me.GetData("SELECT CustomerID, City FROM Customers")
    gvCustomers.DataBind()
    gvOrders.DataSource = Me.GetData("SELECT OrderID, CustomerID FROM Orders")
    gvOrders.DataBind()
End Sub
 
Private Function GetData(ByVal query As String) As DataTable
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As SqlConnection = New SqlConnection(constr)
        Using sda As SqlDataAdapter = New SqlDataAdapter(query, con)
            Using dt As DataTable = New DataTable()
                sda.Fill(dt)
                Return dt
            End Using
        End Using
    End Using
End Function
 
 
Exporting GridViews to Word
When the Export Button is clicked, the Response class properties are set.
1. Content-Disposition – It is a response header indicating, the download file is an attachment and allows setting the file name.
Note: For more details on Content-Disposition header, please refer What is Content Disposition Header in ASP.Net.
 
2. ContentType – It informs the Browser about the file type. In this case it is Word document.
 
Next, the StringWriter and HtmlTextWriter class objects are created, and the AllowPaging property of the referenced GridView is set to the value selected in RadioButton.
An object of Table class is created along with the one TableRow and three TableCell class objects to construct the HTML Table dynamically according to the export preference.
Then, the GridView is added as a cell in separate rows.
A check is performed to know the selected preference according to which the layout of the exported GridView in Word file will be determined.
Finally, StringWriter object is written to the Response which initiates the File download operation.
C#
protected void ExportToWord(object sender, EventArgs e)
{
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.doc");
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-word";
 
    using (StringWriter sw = new StringWriter())
    {
        HtmlTextWriter hw = new HtmlTextWriter(sw);
 
        gvCustomers.AllowPaging = Convert.ToBoolean(rbPaging.SelectedItem.Value);
        gvCustomers.PagerSettings.Visible = false;
        gvCustomers.DataBind();
        gvOrders.AllowPaging = Convert.ToBoolean(rbPaging.SelectedItem.Value);
        gvCustomers.PagerSettings.Visible = false;
        gvOrders.DataBind();
 
        Table table = new Table();
 
        TableRow row1 = new TableRow();
 
        TableCell cell1 = new TableCell();
        cell1.Controls.Add(gvCustomers);
        row1.Cells.Add(cell1);
 
        TableCell cell2 = newTableCell();
        cell2.Text = "&nbsp;";
 
        TableCell cell3 = new TableCell();
        cell3.Controls.Add(gvOrders);
 
        if (rbPreference.SelectedValue == "2")
        {
            row1.Cells.Add(cell2);
            row1.Cells.Add(cell3);
            table.Rows.Add(row1);
        }
        else
        {
            TableRow tr2 = new TableRow();
            tr2.Cells.Add(cell2);
 
            TableRow tr3 = new TableRow();
            tr3.Cells.Add(cell3);
 
            table.Rows.Add(row1);
            table.Rows.Add(tr2);
            table.Rows.Add(tr3);
        }
        table.RenderControl(hw);
 
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();
    }
}
 
VB.Net
Protected Sub ExportToWord(ByVal sender As Object, ByVal e As EventArgs)
    Response.Clear()
    Response.Buffer = True
    Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.doc")
    Response.Charset = ""
    Response.ContentType = "application/vnd.ms-word"
 
    Using sw As StringWriter = New StringWriter()
        Dim hw As HtmlTextWriter = New HtmlTextWriter(sw)
 
        gvCustomers.AllowPaging = Convert.ToBoolean(rbPaging.SelectedItem.Value)
        gvCustomers.PagerSettings.Visible = False
        gvCustomers.DataBind()
        gvOrders.AllowPaging = Convert.ToBoolean(rbPaging.SelectedItem.Value)
        gvOrders.PagerSettings.Visible = False
        gvOrders.DataBind()
 
        Dim table As Table = New Table()
 
        Dim row1 As TableRow = New TableRow()
 
        Dim cell1 As TableCell = New TableCell()
        cell1.Controls.Add(gvCustomers)
        row1.Cells.Add(cell1)
 
        Dim cell2 As TableCell = New TableCell()
        cell2.Text = "&nbsp;"
 
        Dim cell3 As TableCell = New TableCell()
        cell3.Controls.Add(gvOrders)
 
        If rbPreference.SelectedValue = "2"Then
            row1.Cells.Add(cell2)
            row1.Cells.Add(cell3)
            table.Rows.Add(row1)
        Else
            Dim tr2 As TableRow = New TableRow()
            tr2.Cells.Add(cell2)
 
            Dim tr3 As TableRow = New TableRow()
            tr3.Cells.Add(cell3)
 
            table.Rows.Add(row1)
            table.Rows.Add(tr2)
            table.Rows.Add(tr3)
        End If
 
        table.RenderControl(hw)
 
        Response.Output.Write(sw.ToString())
        Response.Flush()
        Response.[End]()
    End Using
End Sub
 
 
Implementing Paging in GridView
Inside the OnPageIndexChanging event handler, the PageIndex property of the GridView is updated with the new Page Number which was clicked.
Finally, the GridView is populated using the BindGrid method which in-turn displays the new GridView page.
C#
protected void OnPaging(object sender, GridViewPageEventArgs e)
{
    GridView gridView = (GridView)sender;
    gridView.PageIndex = e.NewPageIndex;
    gridView.DataBind();
}
 
VB.Net
Protected Sub OnPaging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
    Dim gridView As GridView = CType(sender, GridView)
    gridView.PageIndex = e.NewPageIndex
    gridView.DataBind()
End Sub
 
 
Error
The following error occurs when you try to render a control such as GridView to HTML using the RenderControl method.
Server Error in '/ASP.Net' Application.

Control gvCustomers of type 'GridView' must be placed inside a form tag with runat=server.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Control ' gvCustomers ' of type 'GridView' must be placed inside a form tag with runat=server.
 
 
Solution
The solution to this problem is to override VerifyRenderingInServerForm event handler.
 
 
Screenshots
The Form
Export Multiple GridViews to Word Document in ASP.Net
 
Vertical Preference
Export Multiple GridViews to Word Document in ASP.Net
 
Horizontal Preference
Export Multiple GridViews to Word Document in ASP.Net
 
 
Demo
 
 
Downloads