Click here to Skip to main content
15,946,342 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am working on export excel functionality. In which I am assigning datatable to gridview object and rendering that control

System.Web.UI.WebControls.GridView dgv = new System.Web.UI.WebControls.GridView();

DataTable dtobj = assmnt.OutstandingActionRPT(compprop);
           if (dtobj.Rows.Count > 0)
           {
               try
               {
                   dtobj.Columns.Remove("QuestionID");
                   dtobj.Columns.Remove("UserID");
                   dtobj.Columns.Remove("DID");
                   dtobj.Columns.Remove("AreaofConcern");
                   dtobj.Columns[0].ColumnName = "Statement";

                   dtobj.Columns[1].ColumnName = "Department";
                   dtobj.Columns[2].ColumnName = "Name";
                   //dtobj.Columns[2].ColumnName = "";

               }
               catch (Exception)
               {

               }
               dgv.BorderWidth = 0;

              //dgv.HeaderStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Left;
               dgv.DataSource = dtobj;
               dgv.DataBind();



           }
       }



           Response.ContentType = "application/x-msexcel";
           Response.AddHeader("Content-Disposition", "attachment; filename=ExcelFile.xls"); Response.ContentEncoding = System.Text.Encoding.UTF8;
           StringWriter tw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(tw);
           dgv.RenderControl(hw);



           Response.Write(tw.ToString());
           Response.End();


I have to increase the width of first column as well as assign horizontal allign of header to left.

BUT I am not getting solution

Please let me know the way to do this.
Thanks
Posted

1 solution

i am giving a link i hope it will help you

Applying CSS in ASP.NET GridView[^]

now i give you one simple example for How to apply CSS in Grid

XML
<style type="text/css">
        .categories h1
        {
            font:bold 16px Arial, Sans-Serif;
        }
        .movies
        {
            margin-left:20px;
            margin-bottom:10px;
            width:100%;
        }
    </style>


<asp:GridView
        id="grdMovies"
        CssClass="categories"
        Runat="server">

<Columns>
                                            
                                            
                                            <asp:TemplateField>
                                                <ItemStyle Width="7%" BorderColor="Dodgerblue" BackColor="white"/>
                                                <ControlStyle Width="96%" />
                                                <ItemTemplate>
                                                    <asp:Label ID="lblQId" Font-Size="8pt" Font-Names="Verdana" runat="server" Text='<%# Bind("QuestionID") %>' style="border: none 0px; text-align:left"/>
                                                </ItemTemplate>
                                            </asp:TemplateField>
<Columns>
</asp:GridView>


i think this example will solve your problem
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900