vs2008 asp.net 的 DataGrid 问题:过宽加省略号

needacoder 2012-04-13 06:52:23
下面是我的一个页面的完整代码
问题:
当"标题"这列的内容太长时,如何截断并在尾部显示省略号,而不是扩宽表列

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TopicList.aspx.cs" Inherits="TestList" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1"
onselectedindexchanged="GridView1_SelectedIndexChanged" AllowPaging="True"
CellPadding="4" ForeColor="#333333" GridLines="None">
<RowStyle BackColor="#E3EAEB" />
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="标题" HeaderText="标题" SortExpression="标题"/>
<asp:BoundField DataField="教师姓名" HeaderText="教师姓名" SortExpression="教师姓名" />
<asp:BoundField DataField="发布时间" HeaderText="发布时间" SortExpression="发布时间" />
<asp:ButtonField Text="按钮" />
</Columns>
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:kaoheConnectionString %>"
SelectCommand="SELECT [ID], [标题], [教师姓名], [发布时间] FROM [fengong]">
</asp:SqlDataSource>

</div>
</form>
</body>
</html>
...全文
97 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
laokaizzz 2012-04-13
  • 打赏
  • 举报
回复
楼上说的不错
needacoder 2012-04-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
不会吧,有空格?
if (e.Row.Cells[1].Text.Trim().Length > 10)
{
e.Row.Cells[1].Text = e.Row.Cells[1].Text.Trim().Substring(0, 10) + "...";
}
[/Quote]
好了,大哥
真不错
孟子E章 2012-04-13
  • 打赏
  • 举报
回复
不会吧,有空格?
if (e.Row.Cells[1].Text.Trim().Length > 10)
{
e.Row.Cells[1].Text = e.Row.Cells[1].Text.Trim().Substring(0, 10) + "...";
}
needacoder 2012-04-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
在绑定事件里处理
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[1].Text.Length > 10)
{
e.Row……
[/Quote]
谢谢您百忙中回复
长度超过10的正常
但是长度小于10的也加了省略号,怎么回事呢?
孟子E章 2012-04-13
  • 打赏
  • 举报
回复
在绑定事件里处理
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[1].Text.Length > 10)
{
e.Row.Cells[1].Text = e.Row.Cells[1].Text.Substring(0, 10) + "...";
}
}
}

62,268

社区成员

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

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

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

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