【问题标题】:Align Header Text Center asp:GridView对齐标题文本中心 asp:GridView
【发布时间】:2016-09-21 13:32:51
【问题描述】:

我在 gridview 中显示了一些内容,除了网格对齐之外,一切正常。

我正在尝试将 Header 的文本居中对齐,但没有任何效果。

我试过这个:

 <asp:GridView ID="Gv_Edu" runat="server" BackColor="White" AutoGenerateColumns="false"
   BorderColor="#000000" BorderStyle="Solid" BorderWidth="1px" CellPadding="4"
  ForeColor="Black" OnRowDataBound="Gv_Edu_RowDataBound">
  <Columns>
<asp:TemplateField HeaderText="Sr.No">
<HeaderStyle HorizontalAlign="Center" />
<ItemTemplate>
   <asp:Label runat="server" ID="srlbl" Text='<%#Container.DataItemIndex+1 %>'></asp:Label>
<ItemTemplate>
  <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="10%" />
  </asp:TemplateField>
    <asp:BoundField HeaderText="Degree / Certificate" DataField="deg">
   <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="30%" />
  </asp:BoundField>
    <asp:BoundField HeaderText="Institute" DataField="inst">
    <HeaderStyle HorizontalAlign="Center" />
     <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="20%" />
    </asp:BoundField>
    <asp:BoundField HeaderText="Program Duration" DataField="term">
      <HeaderStyle HorizontalAlign="Center" />
      <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="10%" />
       </asp:BoundField>
      </Columns>
 <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
 </asp:GridView>

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    你能试试这个代码到aspx中的模板字段吗?

    ItemStyle-HorizontalAlign="Center"
    

    【讨论】:

    【解决方案2】:

    有时接受的答案不会起作用(css 是由样式表和/或皮肤文件强制执行的)。 在这种情况下,您可以设置自定义 css 类(如果要对齐特定列):

    .alignRight {
        text-align: right !important;
    }
    

    接下来您需要附加到您的 GridView RowDataBound 事件并在特定列上设置 css:

        protected void SumGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                string forcedCss = "alignRight";
    
                //TODO: change your col index:
                e.Row.Cells[2].CssClass = forcedCss;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-29
      相关资源
      最近更新 更多