繁体   English   中英

如何在GridView中控制列宽?

[英]How to control column width in gridview?

如何使GridView自动调整每列的宽度或固定Default.aspx中每列的宽度? 我遇到的问题是某些列太宽而另一些列太窄(数据转到下一行)。

<div class="divNext">
    <asp:ScriptManagerProxy ID="DisplayResultsScriptManager" runat="server">
    </asp:ScriptManagerProxy>
    <asp:UpdatePanel ID="DisplayResultsUpdatePanel" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <div>
                <asp:GridView ID="GridViewX" RowStyle-Wrap="false" AllowSorting="true"
                    GridLines="Vertical" OnSorting="GridViewX_Sorting" OnRowDataBound="GridViewX_RowDataBound"
                    runat="server" Height="100" Width="100%" EnableViewState="true">
                </asp:GridView>
            </div>
            <asp:Timer ID="ResultsTimer" Interval="60000" Enabled="true" runat="server" OnTick="DisplayResultsTimer_Tick">
            </asp:Timer>
        </ContentTemplate>
    </asp:UpdatePanel>
</div>

如果要对每个列进行更多控制,则需要使用BoundField或其他一些字段。

此页面的声明性语法部分中查找其他类型的字段。

注意 :如果您自己创建列, 确保AutoGenerateColumns="False"

在此处输入图片说明

<asp:GridView ID="GridViewX" runat="server" AutoGenerateColumns="False">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Name">
            <ItemStyle Width="150px"/>
        </asp:BoundField>
        <asp:BoundField DataField="Address" HeaderText="Address">
            <ItemStyle Width="50px"/>
        </asp:BoundField>
    </Columns>
</asp:GridView>

protected void Page_Load(object sender, EventArgs e)
{
    GridViewX.DataSource = new List<Custom>
    {
        new Custom {Name = "Jon Doe", Address = "123 Street"},
        new Custom {Name = "Merry Doe", Address = "123 Street"},
    };
    GridViewX.DataBind(); 
}

public class Custom
{
    public string Name { get; set; }
    public string Address { get; set; }
}

添加<RowStyle CssClass="rowstyle" />

 <asp:GridView ID="GridViewX" RowStyle-Wrap="false" AllowSorting="true"
                    GridLines="Vertical" OnSorting="GridViewX_Sorting" OnRowDataBound="GridViewX_RowDataBound"
                    runat="server" Height="100" Width="100%" EnableViewState="true">
                 <RowStyle CssClass="rowstyle" />
                </asp:GridView> 

CSS:

<style>
        .rowstyle td
        {
            width:150px;
        }

    </style>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM