简体   繁体   English

ASP.NET设置GridView列的最大宽度大小

[英]ASP.NET Set GridView Column max width size

I am using ASP.NET C# on VS2005. 我在VS2005上使用ASP.NET C#。

I have a GridView table with a column named Description , and since the input is always very long, the description is very long horizontally. 我有一个GridView表,其中包含一个名为Description的列,由于输入总是很长,因此水平描述非常长。

I would want the GridView to have a max width size for all columns. 我希望GridView具有所有列的最大宽度大小。

I have tried many ways but none of them worked. 我尝试了很多方法,但没有一个方法奏效。

ItemStyle-Width="50px" , ItemStyle-Width="50px"

HeaderStyle-BorderWidth="50px" , HeaderStyle-BorderWidth="50px"

HeaderStyle-Width="50px" , HeaderStyle-Width="50px"

RowStyle-Width="50px" , RowStyle-Width="50px"

Width="50px"

Below is a code snippet of my GridView: 下面是我的GridView的代码片段:

<asp:GridView ID="GridView1" AutoGenerateEditButton="True" ondatabound="gv_DataBound" runat="server" DataSourceID="SqlDataSource1">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:CheckBox ID="UserSelector" OnCheckedChanged="UserSelector_CheckedChanged" ondatabound="gv_DataBound" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Anyone know how to adjust the size of GridView columns based on my situation? 任何人都知道如何根据我的情况调整GridView列的大小?

This is how I control the column width. 这就是我控制列宽的方法。

1.Add the CSS in the header. 1.在标题中添加CSS。

<style type="text/css">
    .maxWidthGrid
    {
        max-width: 300px;
        overflow: hidden;
    }
</style>

2.Then use the CSS in necessary columns like this ItemStyle-CssClass="maxWidthGrid" 2.然后在必要的列中使用CSS,例如ItemStyle-CssClass="maxWidthGrid"

<asp:BoundField ItemStyle-CssClass="maxWidthGrid" DataField="ClientName" HeaderText="Client Name"
                    ReadOnly="True" SortExpression="ClientName" />

I changed my code is this what you where thinking bout? 我改变了我的代码是你在哪里思考回合?

        <RowStyle Width="150px"/>

Remove all the stash 删除所有藏匿处

<asp:TemplateField><ItemTemplate> </ItemTemplate></asp:TemplateField>

and just use simple 并且只是使用简单

<columns> </columns>

Here i how i think you code will look like AutoGenerateColumns="True" or false dont seeme to matter 在这里,我认为你的代码看起来像AutoGenerateColumns =“True”或false假装不重要

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" DataSourceID="SqlDataSource1">
         <Columns>
            <asp:CheckBox ID="UserSelector" OnCheckedChanged="UserSelector_CheckedChanged" ondatabound="gv_DataBound" runat="server" />
        </Columns>
        <RowStyle Width="150px"/>
    </asp:GridView>

you can handle the RowDataBound event. 你可以处理RowDataBound事件。

protected int widestData;
protected void GridView1_RowDataBound(object sender, 
    GridViewRowEventArgs e)
{
    System.Data.DataRowView drv;
    drv = (System.Data.DataRowView)e.Row.DataItem;
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
      if (drv != null)
      {
        String catName = drv[1].ToString();
        Response.Write(catName + "/");

        int catNameLen = catName.Length;
        if (catNameLen > widestData)
        {
          widestData = catNameLen;
          GridView1.Columns[2].ItemStyle.Width =
            widestData * 30;
          GridView1.Columns[2].ItemStyle.Wrap = false;
        }

      }
    }
}

protected void Page_Load(object sender, EventArgs e)
{
    widestData = 0;
}

Check this How to: Set GridView Web Server Control Column Width Dynamically for more detailed information. 请查看如何:动态设置GridView Web服务器控件列宽以获取更多详细信息。

if your gridview autogeneratecolumns property is set to false and you are creating custom columns then you can use templatefileds where you can implement Table and Div to control the width of the columns. 如果您的gridview autogeneratecolumns属性设置为false并且您正在创建custom columns则可以使用templatefileds ,您可以在其中实现TableDiv来控制列的宽度。

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

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