最新文章
·在ADO使用SELECT语法...
·无组件上传图片之文件...
·用ASP实现邮箱登陆的...
·如何在服务器端调用wi...
·DataGrid自定义分页
·作一个可以直接和浏览...
·MSSQL中时间查询的一...
·我做的可上传下载控件...
·SQL Server性能分析参...
·一个简单网上书城的例...
·用ASP代码创建EXCHANG...
·使用 IIS 5.0 调整 We...
相关文章
·微软 IE5.01 和 Acces...
·一个实用的FSO-实时...
·DataTable中数据记录...
·如何增强ASP程序性能(...
·PivotChart 和 PivotT...
·RC4经典加密算法VB版...
·重写表格--[js源码]...
·email验证
·用err.raise自定义错...
·ASP提速技巧五则
·硬盘文件搜索代码(AS...
·一个检测一个字符串在...
推荐文章
·SQL Server静态页面导...
·故事接龙2 handlevote...
·使用Windows DNA 设计...
·ASP实用函数库源码(...
·WEB打印大全
·存储过程使用大全
·iPlanet Web Server ...
·关于Adodb.Stream的写...
·最简洁的多重查询的解...
·全编辑WebGrid控件Lrc...
·一个BBS的源代码(四)
·用ASP列出服务器上的...
  您现在的位置: 休闲居 >> 网络学院 >> 网络编程 >> ASP >> 

在DataGrid中对内容的截取
  人气: 【字体:大 中 小】
  发布时间:2007-02-20 08:02:03

在使用DataGrid时经常遇到单元格中的内容过长而导致文本的换行,这样使本来简洁的页面看上去非常乱。下面的方法可以解决这个问题。
当单元格的内容超出指定的长度后,截去多余的字,然后在鼠标停留在某个单元格上时,就显示全部的内容。
此方法有个缺点:每个单位格都是指定长度的。
//某个datagrid的ItemDataBound事件。 休 闲 居 编 辑
//上半部分设置鼠标悬停时的背景色
//下半部分才起作用
public void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item || e.Item.ItemType==ListItemType.AlternatingItem)
{
Color color = this.DataGrid1.SelectedItemStyle.ForeColor;
string foreColor = string.Format("#{0:X2}{1:X2}{2:X2}", color.R, color.G, color.B);
color = this.DataGrid1.SelectedItemStyle.BackColor;
string backColor = string.Format("#{0:X2}{1:X2}{2:X2}", color.R, color.G, color.B);
// 如果没有设置选中项的颜色,则不设置鼠标效果
if(foreColor != "#000000" || backColor != "#000000")
{
e.Item.Attributes.Add("onmouseover", string.Format("oldItemForeColor=this.style.color;this.style.color='{0}';oldItemBackColor=this.style.backgroundColor;this.style.backgroundColor='{1}'", foreColor, backColor));
e.Item.Attributes.Add("onmouseout", "this.style.color=oldItemForeColor;this.style.backgroundColor=oldItemBackColor;");
}
e.Item.Cells[1].Text = "<div style='text-overflow : ellipsis; overflow : hidden;width:80px;' title='"+e.Item.Cells[1].Text+"'><nobr>" + e.Item.Cells[1].Text + "</nobr></div>";
e.Item.Cells[2].Text = "<div style='text-overflow : ellipsis; overflow : hidden;width:80px;' title='"+e.Item.Cells[2].Text+"'><nobr><a class=lan href='"+this.Request.ApplicationPath+@"/UpLoadFile/"+System.IO.Path.GetFileName(e.Item.Cells[7].Text)+"'>" + e.Item.Cells[2].Text + "</a></nobr></div>";
e.Item.Cells[3].Text = "<div style='text-overflow : ellipsis; overflow : hidden;width:80px;' title='"+e.Item.Cells[3].Text+"'><nobr>" + e.Item.Cells[3].Text + "</nobr></div>";
e.Item.Cells[4].Text = "<div style='text-overflow : ellipsis; overflow : hidden;width:80px;' title='"+e.Item.Cells[4].Text+"'><nobr>" + e.Item.Cells[4].Text + "</nobr></div>";
e.Item.Cells[5].Text = "<div style='text-overflow : ellipsis; overflow : hidden;width:90px;' title='"+e.Item.Cells[5].Text+"'><nobr>" + e.Item.Cells[5].Text + "</nobr></div>";
e.Item.Cells[6].Text = "<div style='text-overflow : ellipsis; overflow : hidden;width:90px;' title='"+e.Item.Cells[6].Text+"'><nobr>" + e.Item.Cells[6].Text + "</nobr></div>";
}
}

≡ 查看、发表评论 ≡