如何不让 GridView 单元格被撑大??

lxy_lxy 2008-12-31 04:12:41
我设置的GridView中的这列宽度是 416 px ,可是如果这列的内容很长而且文字中间没有空格的话,他就不会自动换行,还是会把 416px 宽度的列给撑大??
怎么解决呢,怎么能让此列中的内容不把列撑大,到了416px长度就自动换行呢``??

<asp:BoundField DataField="IO_Remark" HeaderText="备注" ItemStyle-Width="416px" />
...全文
895 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿彪兄 2009-01-02
  • 打赏
  • 举报
回复
你也可以在rowdatabound事件中截取一段长度,然后多的用....
bj890 2009-01-02
  • 打赏
  • 举报
回复
up
ailin84 2009-01-02
  • 打赏
  • 举报
回复
自动换行问题,正常字符的换行是比较合理的,而连续的数字和英文字符常常将容器撑大,挺让人头疼,下面介绍的是CSS如何实现换行的方法

对于div,p等块级元素
正常文字的换行(亚洲文字和非亚洲文字)元素拥有默认的white-space:normal,当定义的宽度之后自动换行
html
<div id="wrap">正常文字的换行(亚洲文字和非亚洲文字)元素拥有默认的white-space:normal,当定义</div>
css
#wrap{white-space:normal; width:200px; }

1.(IE浏览器)连续的英文字符和阿拉伯数字,使用word-wrap : break-word ;或者word-break:break-all;实现强制断行

#wrap{word-break:break-all; width:200px;}
或者
#wrap{word-wrap:break-word; width:200px;}

<div id="wrap">abcdefghijklmnabcdefghijklmnabcdefghijklmn111111111</div>

效果:可以实现换行

2.(Firefox浏览器)连续的英文字符和阿拉伯数字的断行,Firefox的所有版本的没有解决这个问题,我们只有让超出边界的字符隐藏或者,给容器添加滚动条

#wrap{word-break:break-all; width:200px; overflow:auto;}

<div id="wrap">abcdefghijklmnabcdefghijklmnabcdefghijklmn111111111</div>

效果:容器正常,内容隐藏

对于table

1. (IE浏览器)使用 table-layout:fixed;强制table的宽度,多余内容隐藏

<table style="table-layout:fixed" width="200">
<tr>
<td>abcdefghigklmnopqrstuvwxyz1234567890ssssssssssssss
</td>
</tr>
</table>

效果:隐藏多余内容

2.(IE浏览器)使用 table-layout:fixed;强制table的宽度,内层td,th采用word-break : break-all;或者word-wrap : break-word ;换行

<table width="200" style="table-layout:fixed;">
<tr>
<td width="25%" style="word-break : break-all; ">abcdefghigklmnopqrstuvwxyz 1234567890
</td>
<td style="word-wrap : break-word ;">abcdefghigklmnopqrstuvwxyz 1234567890
</td>
</tr>
</table>

效果:可以换行

3. (IE浏览器)在td,th中嵌套div,p等采用上面提到的div,p的换行方法

4.(Firefox浏览器)使用 table-layout:fixed;强制table的宽度,内层td,th采用word- break : break-all;或者word-wrap : break-word ;换行,使用overflow:hidden;隐藏超出内容,这里overflow:auto;无法起作用

<table style="table-layout:fixed" width="200">
<tr>
<td width="25%" style="word-break : break-all; overflow:hidden; ">abcdefghigklmnopqrstuvwxyz1234567890</td>
<td width="75%" style="word-wrap : break-word; overflow:hidden; ">abcdefghigklmnopqrstuvwxyz1234567890</td>
</tr>
</table>

效果:隐藏多于内容

5.(Firefox浏览器) 在td,th中嵌套div,p等采用上面提到的对付Firefox的方法
zhzhyong17 2009-01-02
  • 打赏
  • 举报
回复
不晓得 也要回个贴
chinacn101 2009-01-01
  • 打赏
  • 举报
回复
/* CSS Document */
body {

word-wrap: break-word;
word-break: break-all;
}
Moon_F 2009-01-01
  • 打赏
  • 举报
回复
<asp:TemplateColumn HeaderText="刪除" HeaderStyle-Width>

表头的列宽就可以了HeaderStyle-Width
mengxj85 2009-01-01
  • 打赏
  • 举报
回复
[Quote=引用楼主 lxy_lxy 的帖子:]
我设置的GridView中的这列宽度是 416 px ,可是如果这列的内容很长而且文字中间没有空格的话,他就不会自动换行,还是会把 416px 宽度的列给撑大??
怎么解决呢,怎么能让此列中的内容不把列撑大,到了416px长度就自动换行呢``??

C# code
<asp:BoundField DataField="IO_Remark" HeaderText="备注" ItemStyle-Width="416px" />
[/Quote]
Itemtype的宽度设为百分比,设换行属性为True,可以不用再自己加脚本
风骑士之怒 2008-12-31
  • 打赏
  • 举报
回复
外面套个表格
wuyq11 2008-12-31
  • 打赏
  • 举报
回复
//正常换行
GridView1.Attributes.Add("style", "word-break:keep-all;word-wrap:normal");
//自动换行
GridView1.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
koukoujiayi 2008-12-31
  • 打赏
  • 举报
回复
最简单的办法:
改成模板列,将Label替换成TextBox,设置TextBox的背景色,去掉边框,设置成只读,
效果和Label一样,不会撑大了!!
  • 打赏
  • 举报
回复
回帖是一种美德!
xfreyes 2008-12-31
  • 打赏
  • 举报
回复
e.Row.Cells[6].Attributes.Add("style", "word-break :break-all ; word-wrap: break-all");

这样就可以了
lxy_lxy 2008-12-31
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 glt3260053 的回复:]
e.Row.Cells[6].Attributes.Add("style", "word-break :break-all ; word-wrap:break-word");

且Wrap="True"
http://www.cnblogs.com/cnaspnet/archive/2008/09/27/1300363.html
[/Quote]
不行,比如此列中的内容是这样:"ss点点滴滴是打发士大夫是打发士大夫是打发士大夫是大方是打发士大夫是打发士大夫是大方是打发士大夫是s"
中间没有一个空格,他就不会换行,会把单元格撑大`~!!怎么办呢??
wangying110166 2008-12-31
  • 打赏
  • 举报
回复
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//这里不知道什么意思,大概是行的状态(是不是可以编辑)
if (e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate)
|| e.Row.RowState == DataControlRowState.Edit)
{
//定义一个文本筐
TextBox curText;
//有多少列,循环几次
for (int i = 0; i < e.Row.Cells.Count; i++)
{

if (e.Row.Cells[i].Controls.Count != 0)
{
e.Row.Cells[6].Attributes.Add("style", "word-break :break-all ; word-wrap:break-word");
}
}
}
}
glt3260053 2008-12-31
  • 打赏
  • 举报
回复
e.Row.Cells[6].Attributes.Add("style", "word-break :break-all ; word-wrap:break-word");

且Wrap="True"
http://www.cnblogs.com/cnaspnet/archive/2008/09/27/1300363.html
djf_1985 2008-12-31
  • 打赏
  • 举报
回复
应该可以啊
要不你试试这样
<asp:BoundField DataField="processdate" HeaderText="處 理 時 間" ReadOnly="True">
<ItemStyle Width="50px" />
dengchenlu 2008-12-31
  • 打赏
  • 举报
回复
设置列宽
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//这里不知道什么意思,大概是行的状态(是不是可以编辑)
if (e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate)
|| e.Row.RowState == DataControlRowState.Edit)
{
//定义一个文本筐
TextBox curText;
//有多少列,循环几次
for (int i = 0; i < e.Row.Cells.Count; i++)
{

if (e.Row.Cells[i].Controls.Count != 0)
{

curText = e.Row.Cells[i].Controls[0] as TextBox;

if (curText != null)
{

curText.Width = Unit.Pixel(70);
}
}
}
}

}

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧