It seem that you are having TemplateField. Please use BoundField instead.
And you have to save the file first and then open it.
HTML:
<form id="form1" runat="server">
<div>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
Name
</td>
<td>
<asp:TextBox ID="txtName" runat="server" />
</td>
</tr>
<tr>
<td>
LastName
</td>
<td>
<asp:TextBox ID="txtLastName" runat="server" />
</td>
</tr>
<tr>
<td>
Cell
</td>
<td>
<asp:TextBox ID="txtCell" runat="server" />
</td>
</tr>
<tr>
<td colspan="2" align="left">
</td>
</tr>
</table>
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" Font-Names="Arial"
Font-Size="11pt" AlternatingRowStyle-BackColor="#C2D69B" HeaderStyle-BackColor="green">
<Columns>
<asp:BoundField ItemStyle-Width="150px" DataField="Name" HeaderText="Name" />
<asp:BoundField ItemStyle-Width="150px" DataField="LastName" HeaderText="LastName" />
<asp:BoundField ItemStyle-Width="150px" DataField="Cell" HeaderText="Cell" />
</Columns>
</asp:GridView>
<asp:Button ID="btnExportToWord" runat="server" Text="Export to Word" OnClick="ExportToWord" />
<br />
<asp:Button Text="Add" OnClick="Add" runat="server" />
</div>
</form>
You need to add this code in the page load without this GridView will not be exported:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["myDatatable"] != null)
{
DataTable dt = (DataTable)Session["myDatatable"];
GridView1.Visible = true;
GridView1.DataSource = dt;
GridView1.DataBind();
btnExportToWord.Visible = true;
}
}
Other function and event.
public void AddNewData()
{
DataTable dt = new DataTable();
if (Session["myDatatable"] != null)
{
dt = (DataTable)Session["myDatatable"];
}
else
{
dt.Columns.Add("Name");
dt.Columns.Add("LastName");
dt.Columns.Add("Cell");
}
DataRow drow = dt.NewRow();
drow["Name"] = this.txtName.Text.Trim();
drow["LastName"] = this.txtLastName.Text.Trim();
drow["Cell"] = this.txtCell.Text.Trim();
dt.Rows.Add(drow);
Session["myDatatable"] = dt;
this.txtName.Text = string.Empty;
this.txtLastName.Text = string.Empty;
this.txtCell.Text = string.Empty;
}
public void BindMyGridview()
{
if (Session["myDatatable"] != null)
{
DataTable dt = (DataTable)Session["myDatatable"];
if ((dt != null) && (dt.Rows.Count > 0))
{
GridView1.Visible = true;
GridView1.DataSource = dt;
GridView1.DataBind();
btnExportToWord.Visible = true;
}
else
{
GridView1.Visible = false;
btnExportToWord.Visible = false;
}
}
}
protected void Add(object sender, EventArgs e)
{
AddNewData();
BindMyGridview();
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
protected void ExportToWord(object sender, EventArgs e)
{
Session["myDatatable"] = null;
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.doc");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-word ";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataBind();
GridView1.RenderControl(hw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
Image:

Thank You.