gridview设置列宽 由于其条数据的长度太长,使得用gridview绑定时,改变了gridview的格局,相当难看,我现在想设置一列的宽度,而且想使超过宽度的字用省略号代替,不知道怎么设置,请各位指教。我用过itemstyle设置width与heigh,但是没有效果,字还是全部显示出来。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 处理rowDataBound事件。if(e.row.rowState==dataControlRowState.alternate||e.row.rowState==dataControlRowState.normal){...} <asp:TemplateField HeaderText="标题"> <ItemTemplate> <a href="ShowRules.aspx?ID=<%#Eval("ID")%>&Page=<%=pageindex%>"> <%#CutString(Eval("Title").ToString(), 40)%> </a> </ItemTemplate> <ItemStyle HorizontalAlign="left" Width="50%" /> </asp:TemplateField> public string CutString(string str, int length) { string delsqace = str.Trim(); int i = 0, j = 0; foreach (char chr in delsqace) { if ((int)chr > 127) { i += 2; } else { i++; } if (i > length) { delsqace = delsqace.Substring(0, j) + "...."; break; } j++; } return delsqace; } <asp:TemplateField> <HeaderTemplate>留言内容 </HeaderTemplate> <ItemTemplate><asp:HiddenField Value='<%#Eval("content") %>' ID="HFcontent" runat="server" /> <asp:Literal runat="server" ID="ltContent" ></asp:Literal></ItemTemplate></asp:TemplateField> protected void grLeaveWord_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { if (e.Row.RowType != DataControlRowType.Header && e.Row.RowType != DataControlRowType.Footer) { //判断数据大于50,大于50后面加否则则全显示 Literal ltContetn = e.Row.Cells[2].FindControl("ltContent") as Literal; if (HFcontent.Value.Length > 50) { ltContetn.Text = content.ToString().PadRight(50).Substring(0, 50) + "...."; } else { ltContetn.Text = content.ToString(); } } } } 在 这个gridview 所在表格 样式中设置 table-layout: fixed;固定表格。然后在每个头行的列中设置宽度,并设置该列 Wrap=false 不换行 1楼的能把那里面的代码给出来一下吗?我是用的hyperlinkfield,不是用的模板,我该怎么显示省略号啊 ,各位高手麻烦给出下代码啊!小弟在此多谢了 //设置不换行 GridView1.Attributes.Add("style", "word-break:keep-all;word-wrap:False"); 在RowDataBound事件下写以下代码if (e.Row.RowType == DataControlRowType.DataRow) { if (e.Row.Cells[17].Text.Length > 10) { e.Row.Attributes.Add("style", "word-break:keep-all;word-wrap:False"); e.Row.Cells[17].Text = e.Row.Cells[17].Text.Substring(0, 10) + "..."; } } combox.selectindex=-1 不能初始化下拉框 关于datagridview导入Excel模板的问题 已知服务器的IP地址,C#如何获得服务器的计算机名称 什么情况下关闭Socket,才会不产生异常? 求ado.net电子书!马上揭帖!! socket 求解C#中Xml注释问题? $$$$讨论热门流行最吃香月薪高的编成语言$$$$ 如何注册一个系统热键(在线等) GDI绘图和多线程的问题 C#里怎么控制timer控件? 各位牛人请帮帮忙,关于winform控件透明背景色的问题!
<ItemTemplate>
<a href="ShowRules.aspx?ID=<%#Eval("ID")%>&Page=<%=pageindex%>">
<%#CutString(Eval("Title").ToString(), 40)%>
</a>
</ItemTemplate>
<ItemStyle HorizontalAlign="left" Width="50%" />
</asp:TemplateField> public string CutString(string str, int length)
{
string delsqace = str.Trim();
int i = 0, j = 0;
foreach (char chr in delsqace)
{
if ((int)chr > 127)
{
i += 2;
}
else
{
i++;
}
if (i > length)
{
delsqace = delsqace.Substring(0, j) + "....";
break;
}
j++;
}
return delsqace; }
<HeaderTemplate>留言内容 </HeaderTemplate>
<ItemTemplate>
<asp:HiddenField Value='<%#Eval("content") %>' ID="HFcontent" runat="server" />
<asp:Literal runat="server" ID="ltContent" ></asp:Literal>
</ItemTemplate></asp:TemplateField>
protected void grLeaveWord_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowType != DataControlRowType.Header && e.Row.RowType != DataControlRowType.Footer)
{
//判断数据大于50,大于50后面加否则则全显示
Literal ltContetn = e.Row.Cells[2].FindControl("ltContent") as Literal;
if (HFcontent.Value.Length > 50)
{
ltContetn.Text = content.ToString().PadRight(50).Substring(0, 50) + "....";
}
else
{
ltContetn.Text = content.ToString();
}
}
}
}
固定表格。
然后在每个头行的列中设置宽度,并设置该列 Wrap=false 不换行
我是用的hyperlinkfield,不是用的模板,我该怎么显示省略号啊 ,各位高手麻烦给出下代码啊!
小弟在此多谢了
GridView1.Attributes.Add("style", "word-break:keep-all;word-wrap:False");
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[17].Text.Length > 10)
{
e.Row.Attributes.Add("style", "word-break:keep-all;word-wrap:False");
e.Row.Cells[17].Text = e.Row.Cells[17].Text.Substring(0, 10) + "...";
}
}