简体   繁体   English

手动设置Gridview的列宽

[英]Set column width of Gridview manually

In my Gridview in ASP.NET the width of a column is determined by the headertext (or the content in the cells). 在ASP.NET中的Gridview中,列的宽度由标题文本(或单元格中的内容)确定。 In my case I have a column with a checkbox where I place the column header vertical, but the column width changes according to the headertext. 在我的情况下,我有一列带有复选框的列,该列将列标题垂直放置,但列宽根据标题文本而变化。 So I would like to do is manually to have a small column. 所以我想做的是手动有一小列。 I tried the standard thing shown in the code below. 我尝试了下面代码中显示的标准内容。

     <asp:TemplateField AccessibleHeaderText="Geen" HeaderText="Geen trans" HeaderStyle-CssClass="VerticalHeader" ItemStyle-Width="10" ItemStyle-Wrap="false" HeaderStyle-Width="10" HeaderStyle-Wrap="false" HeaderStyle-HorizontalAlign="Left" >
        <ItemTemplate>
            <asp:CheckBox ID="cbNoTransport" runat="server" AutoPostBack="true" OnCheckedChanged="cbNoTransport_OnCheckedChanged"/>
        </ItemTemplate>
    </asp:TemplateField>

     <div id="grid" style="overflow:scroll; overflow-Y:hidden;" >
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
    DataKeyNames="Barcode" DataSourceID="SqlDataSource1" 
    EmptyDataText="There are no data records to display." AllowPaging="True" 
    AllowSorting="True" onrowcancelingedit="GridView1_RowCancelingEdit" 
    onrowdeleted="GridView1_RowDeleted" onrowediting="GridView1_RowEditing" 
    onrowupdated="GridView1_RowUpdated" onsorted="GridView1_Sorted" 
    onpageindexchanged="GridView1_PageIndexChanged" PageSize="30"
    CssClass="Grid" onrowcommand="GridView1_RowCommand" 
    onrowdatabound="GridView1_RowDataBound" >
<Columns>
    <asp:BoundField DataField="Barcode" HeaderText="Barcode" 
        SortExpression="Barcode">
    </asp:BoundField>
    <asp:BoundField DataField="Werkpakket" HeaderText="Werkpakket" 
        SortExpression="Werkpakket">
    </asp:BoundField>
    <asp:BoundField DataField="HA" HeaderText="HA" SortExpression="HA" />
    <asp:BoundField DataField="Discipline" HeaderText="Discipline" 
        SortExpression="Discipline" >
    </asp:BoundField>
    <asp:BoundField DataField="Type" HeaderText="Type" 
        SortExpression="Type" >
    </asp:BoundField>
    <asp:BoundField DataField="Apparaat" HeaderText="Apparaat" 
        SortExpression="Apparaat" />
    <asp:TemplateField AccessibleHeaderText="Geen" HeaderText="Geen trans" HeaderStyle-CssClass="VerticalHeader" ItemStyle-Width="1%" ItemStyle-Wrap="false" HeaderStyle-Width="1%" HeaderStyle-Wrap="false" HeaderStyle-HorizontalAlign="Left" >
        <ItemTemplate>
            <asp:CheckBox ID="cbNoTransport" runat="server" AutoPostBack="true" OnCheckedChanged="cbNoTransport_OnCheckedChanged"/>
        </ItemTemplate>
        <ItemStyle Width="5%" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Lijn dienst" HeaderStyle-CssClass="VerticalHeader">
        <ItemTemplate>
            <asp:CheckBox ID="cbNoTransport" runat="server" AutoPostBack="true" OnCheckedChanged="cbNoTransport_OnCheckedChanged" />
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Intercity" HeaderStyle-CssClass="VerticalHeader">
        <ItemTemplate>
            <asp:CheckBox ID="cbNoTransport" runat="server" AutoPostBack="true" OnCheckedChanged="cbNoTransport_OnCheckedChanged"/>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="Appendage" HeaderText="Appendage" 
        SortExpression="Appendage" />
    <asp:BoundField DataField="Leiding" HeaderText="Leiding" 
        SortExpression="Leiding" />
    <asp:BoundField DataField="user_added" HeaderText="user_added" 
        SortExpression="user_added" />
    <asp:BoundField DataField="DTPrinted" HeaderText="DTPrinted" 
        SortExpression="DTPrinted" >
    </asp:BoundField>
    <asp:BoundField DataField="DTUpdated" HeaderText="DTUpdated" 
        SortExpression="DTUpdated" />
</Columns>
    <HeaderStyle HorizontalAlign="Left" Wrap="False" />
    <RowStyle HorizontalAlign="Left" Wrap="False" />

EDITED with code of gridview. 使用gridview代码进行编辑。

Can anyone help me with this? 谁能帮我这个?

What about adding "px" as pixel or "%" to the width amount? 将“ px”作为像素或“%”添加到宽度量该怎么办? Like: 喜欢:

ItemStyle-Width="10px" HeaderStyle-Width="10px"

I suppose that unit of measure is needed. 我认为需要计量单位。

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

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