Sea*_*dge 1 c# asp.net gridview
如何在列数据上设置省略号
我的BoundFieldGridView中有以下内容:
<asp:BoundField DataField="Provider" HeaderText="Provider" ItemStyle-VerticalAlign="Top" ItemStyle-CssClass="hideText" ItemStyle-Width="100" />
Run Code Online (Sandbox Code Playgroud)
它是从一个SQL查询填充并显示如下(污迹数据是从结果返回的所有提供程序):

我正在从代码隐藏(部分代码)填充数据:
int rowcounter = 0;
SPListItemCollection collListItems = list.GetItems(oQuery);
foreach (SPListItem item in collListItems)
{
try
{
rowcounter++;
string decoded = HttpUtility.HtmlDecode(item["Guideline"].ToString());
string location = item["Location"].ToString().Replace("#", "\n").TrimStart(';');
string specialty = item["Specialty"].ToString().Replace("#", "\n").TrimStart(';');
string topic = item["Topic"].ToString().Replace("#", "\n").TrimStart(';');
strTopNum = topic.Split(';')[0]; //gets the number for the topic index to display it in a button
//MessageBox.Show(strTopNum);
//var btn = (System.Web.UI.WebControls.IButtonControl)
string provider = item["Provider"].ToString().Replace("#", "\n").TrimStart(';');
Results.Rows.Add(item["ID"], location.TrimEnd(';'), specialty.TrimEnd(';'), topic.Split(';')[1], provider.TrimEnd(';'), item["Summary"].ToString(), item["Guideline"].ToString());
//.Split(';')[1] for topic
//Results.Rows.Add(item["ID"], item["Location"].ToString().Replace("#", "\n").TrimStart(';').TrimEnd(';'), item["Specialty"].ToString().Replace("#", "\n").TrimStart(';').TrimEnd(';'), item["Topic"].ToString().Replace("#", "\n"), item["Provider"].ToString().Replace("#", "\n"), item["Summary"].ToString(), item["Guideline"].ToString());
//strNum[item] = topic.Split(';')[0];
Results.DefaultView.Sort = Results.Columns[3].ColumnName + " ASC";
Results = Results.DefaultView.ToTable(true);
}
catch (Exception ex)
{
string error = ex.Message;
}
}
Run Code Online (Sandbox Code Playgroud)
如何修改代码,以便PROVIDER列只显示前3条记录,...如果有更多记录返回,则后跟?
我使用了以下CSS,但它对我不起作用:
.hideText {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
Run Code Online (Sandbox Code Playgroud)
更新到:
<asp:TemplateField>
<HeaderTemplate>
<asp:Label runat="server" ID="lblProvider" Text="Provider" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label runat="server" ID="lblPro" Text='<%#Eval("Provider")%>' ToolTip='<%#Eval("Provider")%>' CssClass="hideText" />
</ItemTemplate>
</asp:TemplateField>
.hideText {
width:50px;
overflow:hidden;
text-overflow:ellipsis;
white-space:nowrap;
}
Run Code Online (Sandbox Code Playgroud)
TD扩展很长,HTML源代码显示:

text-overflow:ellipsis; 仅在以下属性为真时才有效:
必须指定元素的宽度.元素必须具有overflow:hidden和white-space:nowrap设置.在css下面改变你的喜好
.hideText {
width:120px;
overflow:hidden;
text-overflow:ellipsis;
white-space:nowrap;
}
Run Code Online (Sandbox Code Playgroud)
这是工作演示
更新:
这是您的完整TemplateField定义.定义div内TemplateField,并与省略的CSS属性装饰.
<asp:TemplateField HeaderText="Provider Name">
<ItemTemplate>
<div style="width: 100px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">
<asp:Label ID="lblEllipsis" runat="server" Text='<%#Eval("Provider") %>' ToolTip='<%#Eval("Provider") %>'></asp:Label>
</div>
</ItemTemplate>
</asp:TemplateField>
Run Code Online (Sandbox Code Playgroud)
这是我在本地gridview中尝试这个时的样子.

| 归档时间: |
|
| 查看次数: |
1918 次 |
| 最近记录: |