Remove Column From GridView While Converting To Excel
<p>I have a GridView And in a Code Behind i am converting it into Excel Sheet on a Button Click Event in the page.........The Code is as follows in aspx.</p>

<pre><code>&lt;form id="form1" runat="server"&gt;
    &lt;table width="1100" border="0" cellpadding="0" cellspacing="0" style="height:100%; background-color:White" align="center"&gt;
            &lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;
            &lt;tr&gt;
                &lt;td align="center"&gt;
                    &lt;asp:Button ID="DividendEportToExcel" runat="server" Text="Convert To Excel" 
                        CssClass="bluesome" onclick="DividendEportToExcel_Click" /&gt;      
                &lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                &lt;td&gt;
                     &lt;asp:GridView ID="MemberDividendView" runat="server"&gt;
                               &lt;/asp:GridView&gt;
               &lt;/td&gt;
           &lt;/tr&gt;
     &lt;/table&gt;
&lt;/form&gt;
</code></pre>

<p>And in the Code Behind I am doing as follows :</p>

<pre><code>protected void DividendEportToExcel_Click(object sender, EventArgs e)
    {
        MemberDividendView.Columns.RemoveAt(5);
        MemberDividendView.ShowHeader = true;
        MemberDividendView.GridLines = GridLines.Both;
        MemberDividendView.PagerSettings.Visible = false;
        MemberDividendView.DataBind();
        ChangeControlsToValue(MemberDividendView);
        Response.ClearContent();
        Response.AddHeader("content-disposition", "attachment; filename=AdjustmentDetialsToExcel.xls");
        Response.ContentType = "application/excel";
        StringWriter sWriter = new StringWriter();
        HtmlTextWriter hTextWriter = new HtmlTextWriter(sWriter);
        HtmlForm hForm = new HtmlForm();
        MemberDividendView.Parent.Controls.Add(hForm);
        hForm.Attributes["runat"] = "server";
        hForm.Controls.Add(MemberDividendView);
        hForm.RenderControl(hTextWriter);
        Response.Write(sWriter.ToString());
        Response.End();
      }

    private void ChangeControlsToValue(Control gridView)
    {
        Literal literal = new Literal();

        for (int i = 0; i &lt; gridView.Controls.Count; i++)
        {
            if (gridView.Controls[i].GetType() == typeof(LinkButton))
            {

                literal.Text = (gridView.Controls[i] as LinkButton).Text;
                gridView.Controls.Remove(gridView.Controls[i]);
                //gridView.Controls.AddAt(i,literal);
            }
            else if (gridView.Controls[i].GetType() == typeof(DropDownList))
            {
                literal.Text = (gridView.Controls[i] as DropDownList).SelectedItem.Text;
                gridView.Controls.Remove(gridView.Controls[i]);
                //gridView.Controls.AddAt(i,literal);
            }
            else if (gridView.Controls[i].GetType() == typeof(CheckBox))
            {
                literal.Text = (gridView.Controls[i] as CheckBox).Checked ? "True" : "False";
                gridView.Controls.Remove(gridView.Controls[i]);
                //gridView.Controls.AddAt(i,literal);
            }
            if (gridView.Controls[i].HasControls())
            {
                ChangeControlsToValue(gridView.Controls[i]);

            }
        }
    }
</code></pre>

<p>BUt still my column is not being deleted in the Excel Sheet........
What is the error ??</p>
