GridView中顯示的列內(nèi)容里字符太長,怎樣顯示截取部分字符并以"..."結(jié)尾
之前的項目中遇到過一個問題,就是gridview中,綁定數(shù)據(jù)源以后,有部分字段內(nèi)容過長,導(dǎo)致表格被撐開變形
當(dāng)時為了排除這個問題,查了一些方法,最終選擇了一種我覺得比較好用的方法,就是在RowDataBound的時候?qū)懘a進(jìn)行處理
下面是我的前臺和后臺代碼,特此記錄:
前臺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">其它學(xué)歷信息</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="添加學(xué)歷信息">添加學(xué)歷</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="學(xué)習(xí)單位/學(xué)校">
<HeaderStyle Wrap="False"/>
<ItemStyle Wrap="False"/>
</asp:HyperLinkColumn>
<asp:BoundColumn DataField="EduCate"HeaderText="學(xué)歷">
<HeaderStyle Wrap="False"/>
<ItemStyle Wrap="False"/>
</asp:BoundColumn>
<asp:BoundColumn DataField="HighestMajor"HeaderText="專業(yè)">
<HeaderStyle Wrap="False"/>
<ItemStyle Wrap="False"/>
</asp:BoundColumn>
<asp:BoundColumn DataField="HTeacherName"HeaderText="導(dǎo)師">
<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="結(jié)束時間">
<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="專業(yè)類型">
<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函數(shù)的編寫,
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
其中涉及到一個我自己寫的字符串處理函數(shù)下面是源碼:
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
當(dāng)然還有另一種處理方法,就是在數(shù)據(jù)庫獲取的時候就進(jìn)行處理,這種方法也很好用,下面是源碼:
company=case when len(company)>10 then left(company,10)+'...' else company end
其中Company就是列名,后面發(fā)現(xiàn)了這種數(shù)據(jù)庫處理方法以后,我基本都用這種方法進(jìn)行處理了,因為實在是比在后臺里處理方便快捷太多