Surya sasidhar
Surya sasidhar

Reputation: 30343

How to make text aligin in Gridview?

In Asp.Net, how to apply text align [left,center,right] in a column, when autogenerate column is true. In every row of gridview the text is displayed in center of the column, i want to display in left side of the column. Thank you.

Upvotes: 10

Views: 77460

Answers (4)

Sorry, this case will work only when autogeneratedcolumns = "false". See the edit for the autogeneratecolumns="true" If you want to align the header of the column add this to the BoundFiled:

 HeaderStyle-HorizontalAlign="Right"

And if you want to align the item of the column add this to the BoundField:

 ItemStyle-HorizontalAlign="Right" 

EDIT:

Also try this too Click on the gridView->properties->RowStyle:horizontal-align

<asp:gridview id="CustomersGridView" 
    datasourceid="CustomersSqlDataSource"  

    autogeneratecolumns="true"

    allowpaging="true"
    ondatabound="CustomersGridView_DataBound"  
    runat="server">

    <pagerstyle forecolor="Blue"
      backcolor="LightBlue"/>

    <pagertemplate>

      <table width="100%">                    
        <tr>                        
          <td style="width:70%">

            <asp:label id="MessageLabel"
              forecolor="Blue"
              text="Select a page:" 
              runat="server"/>
            <asp:dropdownlist id="PageDropDownList"
              autopostback="true"
              onselectedindexchanged="PageDropDownList_SelectedIndexChanged" 
              runat="server"/>

          </td>   

          <td style="width:70%; text-align:right">

            <asp:label id="CurrentPageLabel"
              forecolor="Blue"
              runat="server"/>

          </td>

        </tr>                    
      </table>

    </pagertemplate> 

  </asp:gridview>

more info on this link

Upvotes: 11

Harsh Baid
Harsh Baid

Reputation: 7249

Try using below syntax if all the columns needs to left aligned

<RowStyle HorizontalAlign="Left"></RowStyle>

Also check these MSDN resource which has various examples for GridView formatting and Code Project article which shows examples for AutoGenerated Columns scenerio.

Upvotes: 20

Icarus
Icarus

Reputation: 63970

You can define <RowStyle> and <HeaderStyle> elements in your markup.

Example:

 <asp:GridView ID="productGridView" Runat="server" 
      DataSourceID="productsDataSource"
        DataKeyNames="ProductID" AutoGenerateColumns="False" 
         BorderWidth="1px" BackColor="#DEBA84" 
         CellPadding="3" CellSpacing="2" BorderStyle="None" 
         BorderColor="#DEBA84">
        <FooterStyle ForeColor="#8C4510" 
          BackColor="#F7DFB5"></FooterStyle>
        <PagerStyle ForeColor="#8C4510" 
          HorizontalAlign="Center"></PagerStyle>
        <HeaderStyle ForeColor="White" Font-Bold="True" 
          BackColor="#A55129"></HeaderStyle>

    </asp:GridView>

Upvotes: 2

Arun Rana
Arun Rana

Reputation: 8584

Put your columns as template fields as per below and give ItemStyle-HorizontalAlign="Left"

 <Columns>
  <asp:TemplateField ItemStyle-Width="5%" HeaderText="No." ItemStyle- 
     HorizontalAlign="Left">
     <ItemTemplate>
     <%# Container.DataItemIndex + 1 %>
     </ItemTemplate>
     </asp:TemplateField>
 </Columns>

So it will work in all browser.

Upvotes: 1

Related Questions