5

你好!谁能告诉我如何将任何放置Gridview在中心Div或面板中?我已经应用了以下 CSS,但它不起作用:

<asp:Panel ID="pnlGrid" CssClass="panel" runat="server">
 <div style="text-align:center">
   <asp:GridView ID="grdReqApproval" runat="server"   AutoGenerateColumns="false"     CssClass="SimpleGrid">
      <Columns>
  <asp:BoundField DataField="Approved By" HeaderText="Approved By" />
  <asp:BoundField DataField="User Name" HeaderText="User Name" />
  <asp:BoundField DataField="Approval Time" HeaderText="Approvel Time" />
</Columns>
</asp:GridView>
</div>
    </asp:Panel>
.panel
{
width: 330px;
padding: 10px;
min-height: 20px;
border: 1px solid #dcdcdc;
margin-left:auto;
margin-right:auto;
}
4

6 回答 6

15

HorizontalAlignGridview的属性可以解决你的问题

    <asp:Panel ID="pID" runat="server">
     <div>
       <asp:GridView ID="gvID" runat="server" AutoGenerateColumns="false"
 HorizontalAlign="Center">
          <Columns>
             ...
             ...
          </Columns>
    </asp:GridView>
    </div>
于 2015-07-02T12:43:47.720 回答
2

检查这对我有用,最终gridview被转换为表格,因此将以下样式表应用于我应用于表格的gridview

CSS

.centered-table {
   margin-left: auto;
   margin-right: auto;
}

HTML

<div>
<table class="centered-table" border="1">
    <tr><td>Pekin</td> <td>Illinois</td></tr>
    <tr><td>San Jose</td><td>California</td></tr>
</table>
</div>

JsFiddle 演示

于 2013-04-18T13:55:59.707 回答
2

线程死灵的轻微情况,但我遇到了同样的问题并设法通过对齐代码块而不是gridview本身来解决它。

基本上,我将元素的宽度设置为 40%,将左侧设置为 30%(这意味着右侧也是 30%,因为数学),这会将整个区域、文本、网格和所有内容都放在页面的中间。

CSS看起来大致是这样的

GridExample
{
position:absolute;
left:30%;
width:40%;
padding:0;
margin:0;
}
于 2015-05-06T14:45:35.140 回答
0

在你的 div 上 text-align: center

于 2013-04-18T13:41:29.433 回答
0

正如您在这里看到的,您的课程有效。只要确保它的父容器更宽,这样它就可以居中

http://jsfiddle.net/C7ybw/

#container{
    width:800px;
    border:1px solid #000;
}
于 2013-04-18T13:47:09.793 回答
0
.div_text_center {
    text-align: center;
    margin-left: auto;
    margin-right: auto;
}

<div class="div_text_center">
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:Button ID="Button1" runat="server" Text="Button" />
            <asp:GridView ID="GridView1" runat="server" HorizontalAlign="Center">
            </asp:GridView>
        </ContentTemplate>
    </asp:UpdatePanel>
</div>

在此处输入图像描述

于 2019-11-11T10:06:30.063 回答