日期:2014-05-18  浏览次数:20440 次

vs2008 asp.net 的 DataGrid 问题:过宽加省略号
下面是我的一个页面的完整代码
问题:
当"标题"这列的内容太长时,如何截断并在尾部显示省略号,而不是扩宽表列

<%@ 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>


------解决方案--------------------
不会吧,有空格?
if (e.Row.Cells[1].Text.Trim().Length > 10)
{
e.Row.Cells[1].Text = e.Row.Cells[1].Text.Trim().Substring(0, 10) + "...";
}