代码如下
前台
<asp:GridView ID="GridView1" runat="server">
            
        </asp:GridView>
后台
string value = "安监局非拉我了房间了瓦房级啊微积分书法家历史的积分我覅见";
        string tmp = string.Empty;
        DataTable dt = new DataTable();
        for (int i = 0; i < 20; i++)
        {
            tmp = value.Substring((new Random()).Next(10), (new Random()).Next(value.Length - 10));
            if (tmp.Equals(string.Empty))
            {
                i--;
                continue;
            }
            if (!dt.Columns.Contains(tmp))
                dt.Columns.Add(tmp);
            else
                i--;
        }
        dt.Rows.Add();
        GridView1.DataSource = dt;
        GridView1.DataBind();解释下,列是动态的。
运行就知道,会很丑,谁有好的列宽设置解决方案吗。由于数据量很大,希望尽量不要在DataBind中写算法。

解决方案 »

  1.   

    以前我也不知道怎么处理列宽的问题,后来知道在设计试图里面编辑GridView里面有编辑列的一项,在里面就可以选择你任意要编辑的属性,当然也包括列宽 
      

  2.   

    不很清楚你的需求,GridView到客户端就是Table,可以用CSS固定列宽
      

  3.   

    试着在GridView 样式里面设置列宽~~
      

  4.   

    <style type="text/css">
    <!--
    body {
    margin-left: 0px;
    margin-top: 0px;
    margin-right: 0px;
    margin-bottom: 0px;
    }
    .STYLE1 {font-size: 12px}
    .STYLE3 {font-size: 12px; font-weight: bold; }
    .STYLE4 {
    color: #03515d;
    font-size: 12px;
    }
    -->
    </style>
      <table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="b5d6e6">
    <tr>
     <td colspan="4" rowspan="3"  bgcolor="#FFFFFF">
                 <span class="STYLE1">
    你的GridView
    </span>
    </td>
    </tr>
    </table>
    这样就会发现你的
    GridView
    变美观了!
    提供一个思路,样式可以自己设置!
      

  5.   


    <style>
    #GridView1 tr td:first-child { width: auto } /* 控制第1列宽度 */
    #GridView1 tr td:first-child+td { width: auto } /* 控制第2列宽度 */
    #GridView1 tr td:first-child+td+td { width: auto } /* 控制第3列宽度 */
    #GridView1 tr td:first-child+td+td+td { width: auto } /* 控制第4列宽度 */
    #GridView1 tr td:first-child+td+td+td+td { width: auto } /* 控制第5列宽度 */
    #GridView1 tr td:first-child+td+td+td+td+td { width: auto } /* 控制第6列宽度 */
    </style>
      

  6.   

    添加列后遍历设置宽度
    或使用Itemplate
      

  7.   

    谢谢各位帮忙。自己弄了个不够完善的方法,动态设置列宽+CSS。