views:

34

answers:

2

I need to right align the values in a column of my gridview in asp.net 3.5 How can I do that?

                                <asp:GridView ID="gvSearchResults" runat="server" 
                                onpageindexchanging="gvSearchResults_PageIndexChanging" AutoGenerateColumns="False" 
                                CssClass="Grid" AllowPaging="True" PageSize="20" ForeColor="#333333" 
                                GridLines="None" Width="99%" Font-Names="Arial" Font-Size="Small">
                            <Columns>
                            <asp:HyperLinkField Text="Vendor Number" DataNavigateUrlFields="RecID" DataNavigateUrlFormatString="~/Apps/DataForms/Processor/ProcPanel.aspx?RecID={0}" DataTextField="VendorNum">
                                <HeaderStyle HorizontalAlign="Center" />
                                <ItemStyle HorizontalAlign="Center" />
                            </asp:HyperLinkField>
                                <asp:BoundField DataField="VendorName" HeaderText="Vendor Name" HtmlEncode="False" ItemStyle-HorizontalAlign="Left"></asp:BoundField>
                                <asp:BoundField DataField="InvoiceNum" HeaderText="Invoice Number" HtmlEncode="False"></asp:BoundField>
                                <asp:BoundField DataField="InvoiceTot" HeaderText="Invoice Total" HtmlEncode="False" DataFormatString="${0:0,0.00}"></asp:BoundField>
                                <asp:BoundField DataField="InvoiceDate" HeaderText="Invoice Date"  HtmlEncode="False"></asp:BoundField>
                                <asp:BoundField DataField="InvoiceApprover" HeaderText="Invoice Approver"  HtmlEncode="False"></asp:BoundField>
                            </Columns>
                                <RowStyle BackColor="#FFFBD6" ForeColor="#333333"/>
                                <FooterStyle CssClass="GridFooter" />
                                <PagerStyle CssClass="GridPager" ForeColor="White" />
                                <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                                <HeaderStyle CssClass="GridHeader"  />
                            </asp:GridView>
+1  A: 
 <asp:BoundField ItemStyle-HorizontalAlign="Right" />

You might also use HeaderStyle-HorizontalAlign="Right"

OR

<RowStyle HorizontalAlign="Right" />

Alternatively, create a CSS class with the text-align value to right. You can then set the CssClass property of the item to the class.

David Neale
for some reason, neither of the two answers work!
user279521
+1  A: 

I believe you need to use the RowStyle:

<asp:GridView>
   <RowStyle HorizontalAlign="Right" />
</asp:GridView>

You can find some more detail information on RowStyle here.

Abe Miessler
for some reason, neither of the two answers work!
user279521
hmm that's weird, can you post a snippet of your code that shows what you're doing?
Abe Miessler
The RowStyle in your snippet doesn't have the HorizontalAlign in it. Did you remove it when it didn't work or something?
Abe Miessler
I removed it after it didn't work. But the actual culprit was CssClass="Grid". This was overwriting my alignment.
user279521