GridView中显示的列内容里字符太长,怎样显示截取部分字符并以"..."结尾
之前的项目中遇到过一个问题,就是gridview中,绑定数据源以后,有部分字段内容过长,导致表格被撑开变形
当时为了排除这个问题,查了一些方法,最终选择了一种我觉得比较好用的方法,就是在RowDataBound的时候写代码进行处理
下面是我的前台和后台代码,特此记录:
前台js文件:
<table cellpadding="0"cellspacing="0"class="box_fold" runat="server" >
<trclass="box_fold_tr">
<tdnowrap="noWrap"style="padding-left: 5px;">
<a href="javascript:void(0);" onclick="iofoldset(this,'ctl00_Content_tblFold6');">
<img src="/iOffice/img/idt_tle_c.gif" border="0" align="absbottom"alt=""/></a>
<strong id="stOtherEducates" runat="server">其它学历信息</strong>
</td>
<tdalign="right"nowrap="nowrap"style="height: 20px; padding-right: 20px">
<asp:LinkButton ID="lnkAddEducate" runat="server" CssClass="td"><imgsrc="/iOffice/img/add.gif"border="0"align="absmiddle"alt="添加学历信息">添加学历</asp:LinkButton>
</td>
</tr>
<tr>
<tdclass="box_td"colspan="2">
<table id="tblFold6" runat="server" cellspacing="0" cellpadding="0" width="580px">
<tr>
<td>
<asp:DataGrid ID="dgdEducate" runat="server" AutoGenerateColumns="False" DataKeyField="ID" Width="95%">
<Columns>
<asp:TemplateColumn HeaderText="序号">
<HeaderStyle Width="40px"Wrap="False"/>
<ItemStyle HorizontalAlign="Center"/>
<ItemTemplate>
<asp:Label ID="Label1" runat="server"Text='<%#DataBinder.Eval(Container, "ItemIndex")+1 %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:HyperLinkColumn DataNavigateUrlFormatString="javascript:openpage('hrepEduBackground.aspx?EduBGID={0}',690,615,'EducateAddUp')" DataNavigateUrlField="ID"DataTextField="School"HeaderText="学习单位/学校">
<HeaderStyle Wrap="False"/>
<ItemStyle Wrap="False"/>
</asp:HyperLinkColumn>
<asp:BoundColumn DataField="EduCate"HeaderText="学历">
<HeaderStyle Wrap="False"/>
<ItemStyle Wrap="False"/>
</asp:BoundColumn>
<asp:BoundColumn DataField="HighestMajor"HeaderText="专业">
<HeaderStyle Wrap="False"/>
<ItemStyle Wrap="False"/>
</asp:BoundColumn>
<asp:BoundColumn DataField="HTeacherName"HeaderText="导师">
<HeaderStyle Wrap="False"/>
<ItemStyle Wrap="False"/>
</asp:BoundColumn>
<asp:BoundColumn DataField="sfdate"HeaderText="开始时间">
<HeaderStyle Wrap="False"/>
<ItemStyle Wrap="False"/>
</asp:BoundColumn>
<asp:BoundColumn DataField="stdate"HeaderText="结束时间">
<HeaderStyle Wrap="False"/>
<ItemStyle Wrap="False"/>
</asp:BoundColumn>
<asp:BoundColumn DataField="HEducationSector"HeaderText="教育类别">
<HeaderStyle Wrap="False"/>
<ItemStyle Wrap="False"/>
</asp:BoundColumn>
<asp:BoundColumn DataField="HProfessionalType"HeaderText="专业类型">
<HeaderStyle Wrap="False"/>
<ItemStyle Wrap="False"/>
</asp:BoundColumn>
<asp:TemplateColumn>
<ItemStyle />
<ItemTemplate>
<asp:LinkButton ID="lnkRemove"runat="server"CausesValidation="false"CommandName="del" Text="<imgsrc=/iOffice/img/delete.gif border=0 alt=删除 />"></asp:LinkButton>
</ItemTemplate>
<HeaderStyle Width="1%" />
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</td>
</tr>
</table>
</td>
</tr>
</table>
后台代码:
首先是ItemDataBound函数的编写,
Private Sub dgdEducate_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgdEducate.ItemDataBound
If e.Item.Cells.Count - 1 > 0 Then
Dim i As Integer
For i = 0 To e.Item.Cells.Count - 1
Ife.Item.Cells(i).Text <> "" Then
e.Item.Cells(i).Text =newstring(e.Item.Cells(i).Text.ToString, 10)
EndIf
Next
EndIf
End Sub
其中涉及到一个我自己写的字符串处理函数下面是源码:
Private Function newstring(ByVal str AsString, ByValstrlen As Integer)as string
If Len(str) > strlen Then
Ifstrlen > 5 Then
str = Mid(str, 1, strlen - 5) +"..."
Else
str = Mid(str, 1, 3) + "..."
EndIf
End If
Return str
End Function
当然还有另一种处理方法,就是在数据库获取的时候就进行处理,这种方法也很好用,下面是源码:
company=case when len(company)>10 then left(company,10)+'...' else company end
其中Company就是列名,后面发现了这种数据库处理方法以后,我基本都用这种方法进行处理了,因为实在是比在后台里处理方便快捷太多