Asp.net gridview列宽

时间:2015-03-04 10:51:04

标签: c# asp.net gridview

我的代码如下:

<asp:BoundField DataField = "resultId" HeaderText = "Result Id" ItemStyle-Width ="15%" ControlStyle-Width="300px"/>

我已经尝试了ItemStyle-WidthControlStyle-Width来修复我的gridView列宽,但似乎都没有。

如何设置column宽度?

6 个答案:

答案 0 :(得分:0)

<asp:BoundField DataField = "resultId" HeaderText = "Result Id">
   <ItemStyle Width="30px"></ItemStyle>
</asp:BoundField>

答案 1 :(得分:0)

试试这个

<asp:BoundField DataField = "resultId" HeaderText = "Result Id" ItemStyle-Width ="15%" ItemStyle-Wrap="false" ControlStyle-Width="300px"/>

您也可以在px中尝试此操作。

答案 2 :(得分:0)

做这样的事,

<asp:BoundField DataField = "resultId" HeaderText = "Result Id" HeaderStyle-Width="100px" />

答案 3 :(得分:0)

首先,您应该将autogeneratecolumns="false"添加到<asp:GridView>

您的aspx文件应如下所示:

     <asp:GridView ID="GridView1" CssClass="gridview" autogeneratecolumns="False" Width="900" runat="server" HeaderStyle-CssClass="HeaderStyle">
        <Columns>
            <asp:boundfield DataField="field0">
                <HeaderStyle CssClass="HeaderStyle" />
                <ItemStyle CssClass="ItemStyle" />
                <FooterStyle CssClass="HeaderStyle" />
                <ControlStyle CssClass="HeaderStyle" />
           </asp:boundfield>
            <asp:boundfield DataField="field1">
                <HeaderStyle CssClass="HeaderStyle" />
                <ItemStyle CssClass="ItemStyle" />
                <FooterStyle CssClass="HeaderStyle" />
                <ControlStyle CssClass="HeaderStyle" />
           </asp:boundfield>
           .........
        </Columns>
        </asp:GridView>

你的CSS文件可能是这样的:

    .HeaderStyle
        {
            background-color: #900;
            color: White;
            font-weight: bold;
            white-space:nowrap;
            width: 100px;
            max-width: 100px;
            overflow: hidden;
        }

        .ItemStyle
        {
            background-color: #eaebec;
            color: Black;
            font-weight: bold;
            white-space:nowrap;
            width: 100px;
            max-width: 100px;
            overflow: hidden;
        }
.gridview {
    font-family: Arial, Helvetica, sans-serif;
    color: #666;
    font-size: 12px;
    background: #eaebec;
    margin: 20px;
    border: #ccc 1px solid;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
    -moz-box-shadow: 0 1px 2px #d1d1d1;
    -webkit-box-shadow: 0 1px 2px #d1d1d1;
}

你问过c#这个问题,但我会在这里添加VB代码,但是你可以转换它:

    Dim tablo As New DataTable
    Dim row As DataRow
    tablo.Columns.Add("field0", System.Type.GetType("System.String"))
    tablo.Columns.Add("field1", System.Type.GetType("System.String"))
    .......
    For i = 1 to XX
        row("field" + Trim(Str(i))) = "Something"
    Next
    tablo.Rows.Add(row)
    GridView1.Columns(0).HeaderText = "Something Header"
    GridView1.Columns(1).HeaderText = "Something Other Header"
    .......

我认为就是这样。希望它有所帮助。

答案 4 :(得分:0)

在页面加载函数中输入以下代码:

protected void Page_Load(object sender, EventArgs e)
{
    GridView1.Attributes.Add("style", "word-break:break-all; word-wrap:break-word");    
}

然后在Boundfield属性中将属性命名为:

HeaderStyle-Width="100px" 

答案 5 :(得分:-1)

<asp:GridView ID="gvDemo2" runat="server" AutoGenerateColumns="false">
            <AlternatingRowStyle Width="60px" />
            <Columns>
                <asp:BoundField DataField="FileId" HeaderText="Name" ItemStyle-Width="100" />
                <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="100" />
                <asp:BoundField DataField="Number" HeaderText="Number" ItemStyle-Width="100" />
                <asp:BoundField DataField="Email" HeaderText="Email" ItemStyle-Width="100" />
                <asp:BoundField DataField="Date" HeaderText="Date" ItemStyle-Width="100" />
                <asp:BoundField DataField="Message" HeaderText="Message" ItemStyle-Width="100" />
            </Columns>
        </asp:GridView>