C# 为 GridView 列创建最大宽度和换行文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17456778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating max width for GridView column and Wrap Text
提问by David Tunnell
I cant figure out how to define the width of a particular column of a gridview. I am getting this result:
我无法弄清楚如何定义 gridview 的特定列的宽度。我得到这个结果:


As you can see, I am getting substantial overflow. I would like to define a maximum width and wrap the test for this 5th column.
如您所见,我正在大量溢出。我想定义一个最大宽度并包装这第 5 列的测试。
I have tried doing it programatically:
我试过以编程方式做到这一点:
GridView1.Columns[4].ItemStyle.Width = 300;
GridView1.DataBind();
And also in asp.net:
还有在asp.net中:
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" ItemStyle-Width="300px" />
or
或者
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" HeaderStyle-Width="300px">
None of these attempts are having any effect.
这些尝试都没有任何效果。
采纳答案by Shashank Chaturvedi
Use the following css class for grid:
对网格使用以下 css 类:
.gridView
{
table-layout:fixed;
}
and on the column use this css class:
并在列上使用此 css 类:
.col
{
word-wrap:break-word;
}
回答by rgrano
The text in the top last column has no spaces. This means that the last column will not wrap so the table will stretch. This is just like displaying a really big image that is too big for the container or page.
顶部最后一列中的文本没有空格。这意味着最后一列不会换行,因此表格会拉伸。这就像显示一个对于容器或页面来说太大的非常大的图像。

