如何在Gridview中对齐列 [英] How to Align Columns in Gridview
本文介绍了如何在Gridview中对齐列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试对齐GridView的列。我已经阅读了我能找到的每篇文章,并且已经走到极端,将horizontalalign =Center添加到几乎所有可以接受它的标签上。问题是,当它运行时,一切都与左边对齐。我缺少什么?
I am trying to align the columns of my GridView. I have read every article I can find and have gone to the extreme to add horizontalalign="Center" to just about every tag that will accept it. Problem is, that when it runs, everything is aligned to the left. What am I missing?
<asp:GridView ID="gvAgency" runat="server"
AutoGenerateColumns="False"
OnRowEditing="gvAgency_RowEditing"
OnRowCancelingEdit="gvAgency_RowCancelingEdit"
OnRowUpdating="gvAgency_RowUpdating"
OnPageIndexChanging="gvAgency_PageIndexChanging"
OnRowDataBound="gvAgency_RowDataBound"
Width="100%" DataKeyNames="Town" BackColor="White"
BorderColor="#7AC0DA" BorderStyle="Double" BorderWidth="3px"
CellPadding="4" GridLines="Horizontal"
UseAccessibleHeader="False" PageSize="7"
ShowHeaderWhenEmpty="True"
SortedDescendingHeaderStyle-HorizontalAlign="NotSet"
HorizontalAlign="Center" AllowPaging="True">
<RowStyle HorizontalAlign="Center" />
<AlternatingRowStyle BorderStyle="Solid"
HorizontalAlign="Center" />
<Columns>
<asp:TemplateField HeaderText="Town"
SortExpression="Town">
<EditItemTemplate>
<asp:Label ID="txtTown" runat="server"
Text='<%# Eval("Town") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblTown" runat="server"
Text='<%# Bind("Town") %>'></asp:Label>
</ItemTemplate>
<FooterStyle BackColor="#31869B"
HorizontalAlign="Center" />
<HeaderStyle BackColor="#31869B" Width="300px"
HorizontalAlign="Center" />
<ItemStyle Width="300px" HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Lethality Screens">
<EditItemTemplate>
<asp:Label ID="txtScreens" runat="server"
Text='<%# Eval("Danger")%>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblScreens" runat="server"
Text='<%# Eval("Danger")%>'></asp:Label>
</ItemTemplate>
<FooterStyle BackColor="#31869B" />
<HeaderStyle BackColor="#31869B" Width="120px" />
<ItemStyle Width="120px" HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
<EditRowStyle HorizontalAlign="Center" />
<FooterStyle BackColor="#5B5A94" ForeColor="White"
BorderColor="Black" />
<HeaderStyle BackColor="#5B5A94" BorderColor="Black"
Font-Bold="True" ForeColor="White"
HorizontalAlign="Center" />
<PagerStyle BackColor="#31869B" ForeColor="White"
HorizontalAlign="Center" BorderColor="Black"
Font-Bold="True" />
<RowStyle BackColor="White" ForeColor="#333333"
HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#7AC0DA"
Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F7F7F7" />
<SortedAscendingHeaderStyle BackColor="#487575" />
<SortedDescendingCellStyle BackColor="#E5E5E5" />
<SortedDescendingHeaderStyle BackColor="#275353" />
</asp:GridView>
推荐答案
你应该能够添加为TemplateField的一个属性,例如
You should be able to add as an attribute of the TemplateField, e.g.
<asp:TemplateField ItemStyle-HorizontalAlign="Center" ...></asp:TemplateField>
或者,在后面的代码中,你可以设置它,例如
Or, in code behind, you can set it e.g.
gvAgency.Columns(N).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
用适当的列号替换N
replacing N with whatever the appropriate column number is
而不是向每个元素添加属性,只需使用CSS。在最坏的情况下,您可以向某些元素添加CSS类
属性。这就是CSS的目的。
-SA
Instead of adding attributes to each element, just use CSS. In worst case, you can add a CSSclass
attribute to some elements. This is what CSS is designed for.
—SA
这篇关于如何在Gridview中对齐列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文