12

我将 GridView 从工具箱拖放到 Aspx 页面。设计代码如下所示

<asp:GridView ID="gridview1" runat="server"   style="text-align:center;width: 1327px;" 
    CellPadding="4" ForeColor="#333333" GridLines="None" AllowPaging="True" 
    EnableSortingAndPagingCallbacks="True" PageSize="50" AutoGenerateEditButton="True">
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    <EditRowStyle BackColor="#999999" />
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" 
        Height="12px" VerticalAlign="Bottom" Width="12px" Wrap="False" />
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#E9E7E2" />
    <SortedAscendingHeaderStyle BackColor="#506C8C" />
    <SortedDescendingCellStyle BackColor="#FFFDF8" />
    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>

我在数据库中有一个按名称地址的列。该表使用以下 VB.NET 代码绑定到此网格视图

 Dim ds as Data.DataSet
 Dim da As New OleDbDataAdapter("select col1,col2,col3,col4,col5,col6,col7,col8,col9,col10,Address from table, con)
        da.Fill(ds)
        gridviewrealestate.DataSource = ds
        gridviewrealestate.DataBind()

现在,当显示网格视图时,由于地址列中存在大量信息,列彼此非常接近。我想将地址列设置为特定的宽度。请帮助我如何做到这一点。

4

8 回答 8

13
<asp:GridView ID="GridView1" runat="server">
    <HeaderStyle Width="10%" />
    <RowStyle Width="10%" />
    <FooterStyle Width="10%" />
    <Columns>
        <asp:BoundField HeaderText="Name" DataField="LastName" 
            HeaderStyle-Width="10%" ItemStyle-Width="10%"
            FooterStyle-Width="10%" />
    </Columns>
</asp:GridView>
于 2012-06-07T09:57:08.677 回答
6

我知道这是一个老问题,但是当我在寻找相同问题的解决方案时它突然出现了,所以我想我会发布对我有用的东西。

<asp:BoundField DataField="Description" HeaderText="Bond Event" ItemStyle-Width="300px" />

我在我的身上使用了这个ItemStyle-Width属性BoundField ,它工作得很好,我还没有遇到任何问题。

我也不需要在其余代码中添加任何其他内容来完成这项工作。

于 2014-05-15T13:40:57.220 回答
3

设置宽度

HeaderStyle-width

例如 HeaderStyle-width="10%"

于 2018-04-05T19:29:49.450 回答
1

这对我有用。设置 HeaderStyle-Width="5%",在页脚设置文本框宽度 Width="15",同时将 gridview 的宽度设置为 100%。以下是我的gridview 的列之一。

    <asp:TemplateField   HeaderText = "sub" HeaderStyle-ForeColor="White" HeaderStyle-Width="5%">
<ItemTemplate>
    <asp:Label ID="sub" runat="server" Font-Size="small" Text='<%# Eval("sub")%>'></asp:Label>
</ItemTemplate> 
 <EditItemTemplate>
    <asp:TextBox ID="txt_sub" runat="server" Text='<%# Eval("sub")%>'></asp:TextBox>
</EditItemTemplate> 
<FooterTemplate>
    <asp:TextBox ID="txt_sub" runat="server" Width="15"></asp:TextBox>
</FooterTemplate>

于 2016-04-12T07:25:36.147 回答
0

有两个步骤:

  1. 您必须像这样为 GridView 设置适当的宽度:Width="2000px"。
  2. 您可以通过设置 [ItemStyle-Width] 来设置列的宽度,如下所示:ItemStyle-Width="300px"。

** 您可以通过设置固定像素(如“150 px”)或百分比(如“10%”)来设置宽度。

于 2020-05-30T11:42:01.913 回答
0
<asp:BoundField DataField="ElementName" HeaderText="RBI GL Name" ItemStyle-Horizontal ItemStyle-Width="50px" HeaderStyle-Width="50px"/> 
于 2021-03-15T10:54:33.860 回答
0

将 HeaderStyle-Width 和 ItemStyle-width 添加到 TemplateFiels

 <asp:GridView ID="grdCanceled" runat="server" AutoGenerateColumns="false" OnPageIndexChanging="grdCanceled_PageIndexChanging"  AllowPaging="true" PageSize="15"
         CssClass="table table-condensed table-striped table-bordered" GridLines="None">
             <HeaderStyle BackColor="#00BCD4"  ForeColor="White" />
             <PagerStyle CssClass="pagination-ys" />
               <Columns>
            <asp:TemplateField HeaderText="Mobile NO" HeaderStyle-Width="10%" ItemStyle-Width="10%">
                <ItemTemplate>
                  <%#Eval("mobile") %>
                </ItemTemplate>
               </asp:TemplateField>
           <asp:TemplateField HeaderText="Name" HeaderStyle-Width="10%" ItemStyle-Width="10%">
                <ItemTemplate>
                    <%#Eval("name") %>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="City" HeaderStyle-Width="10%" ItemStyle-Width="10%">
                <ItemTemplate>
                    <%#Eval("city") %>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Reason" HeaderStyle-Width="25%" ItemStyle-Width="25%">
                 <ItemTemplate>
                    <%#Eval("reson") %>
                 </ItemTemplate>
             </asp:TemplateField>
             <asp:TemplateField HeaderText="Agent" HeaderStyle-Width="10%" ItemStyle-Width="10%">
                 <ItemTemplate>
                     <%#Eval("Agent") %>
                 </ItemTemplate>
             </asp:TemplateField>
             <asp:TemplateField HeaderText="Date" HeaderStyle-Width="10%" ItemStyle-Width="10%">
                 <ItemTemplate>
                   <%#Eval("date","{0:dd-MMM-yy}") %>
                 </ItemTemplate>
              </asp:TemplateField>
              <asp:TemplateField HeaderText="DList" HeaderStyle-Width="10%" ItemStyle-Width="10%">
                  <ItemTemplate>
                      <%#Eval("service") %>
                  </ItemTemplate>
              </asp:TemplateField>
              <asp:TemplateField HeaderText="EndDate" HeaderStyle-Width="10%" ItemStyle-Width="10%">
                  <ItemTemplate>
                      <%#Eval("endDate","{0:dd-MMM-yy}") %>
                  </ItemTemplate>
              </asp:TemplateField>
              <asp:TemplateField HeaderStyle-Width="5%" ItemStyle-Width="5%">
                  <ItemTemplate>
                       <asp:CheckBox data-needed='<%#Eval("userId") %>' ID="chkChecked" runat="server" />
                   </ItemTemplate>
               </asp:TemplateField>
             </Columns>
          </asp:GridView>
于 2017-04-19T07:06:49.547 回答
0

您需要将该列转换为“模板字段”。

在设计器视图中,单击 GridView 的智能标记,选择->“编辑列”。选择您要修改的列,然后按超链接将其转换为模板。请参阅屏幕截图: 将列转换为模板

注意:修改您的数据源可能会再次重新生成列,因此它可能会更改您的模板列。

参考:https ://msdn.microsoft.com/en-us/library/bb288032.aspx

于 2016-07-29T19:43:02.260 回答