locked
How to display gridLines in GridView from code behind RRS feed

  • Question

  • User-172769993 posted

    In aspx code i set GridLines="None"

    How can i show GridLines from code behind when exporting grid-view to excel file.

    <asp:GridView ID="gridusers" GridLines="None" runat="server" CssClass="table" AutoGenerateColumns="False">
    <Columns>
    </Columns>
    </asp:GridView>

    C#

    protected void btnexcel_Click(object sender, EventArgs e)
    {
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition",
             "attachment;filename=UsersReport.xls");
            Response.Charset = "";
            Response.ContentType = "application/vnd.ms-excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            gridusers.DataBind();
            gridusers.RenderControl(hw);
            //style to format numbers to string
            string style = @"<style> .textmode { mso-number-format:\@; } </style>";
            Response.Write(style);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();
    }

    <asp:GridView runat="server" ID="gridview1" Height="175px" Width="750px" GridLines="Both"
            OnRowDataBound="gvdata_onRowdataBound">
            <Columns>
           
            </Columns>
        </asp:GridView>
    Friday, June 24, 2016 5:05 AM

Answers

  • User541108374 posted

    Hi,

    before rendering the control set the GridLines property to Both:

    So you can try:

    gridusers.GridLines = GridLines.Both;
    gridusers.DataBind();
    gridusers.RenderControl(hw);

    Grz, Kris.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 24, 2016 5:35 AM