gridview绑定的标题列内容过长怎么弄? gridview绑定的标题列一行大概放20个汉字,如果多了自动换行,页面就不美观了怎么让标题需要换行的末尾显示省略号,鼠标移到那显示全部内容 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在RowDataBound事件中进行判断 ,鼠标移到那显示全部内容——————设置该单元格的title属性为标题内容; 用截取法,title属性放置鼠标显示全部内容 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { string sTmp; string pic; if (((DataRowView)e.Item.DataItem)["FileTitle"] != DBNull.Value) { sTmp = Convert.ToString(((DataRowView)e.Item.DataItem)["FileTitle"]); pic = Convert.ToString(((DataRowView)e.Item.DataItem)["IsImage"]); if (pic == "1") { string stmp2 = ""; if (sTmp.Length <= 19) stmp2 = sTmp + "(图)"; if (sTmp.Length > 19) stmp2 = sTmp.Substring(0, 18) + "…(图)"; sTmp = stmp2; } else { if (sTmp.Length > 21) sTmp = sTmp.Substring(0, 20) + "…"; } ((HyperLink)e.Item.FindControl("hylzcwj")).Text = "˙" + sTmp; } } } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e){ if (e.Row.RowType == DataControlRowType.DataRow) { for (int i = 0; i < e.Row.Cells.Count; i++) { if ((e.Row.Cells[i].Text != "") && (i == 5) && (e.Row.Cells[i].Text != null)) { str = e.Row.Cells[i].Text; e.Row.Cells[i].Text = cutStr(e.Row.Cells[i].Text, 20); e.Row.Cells[i].Text = "<a style='cursor:hand;' title='" + str + "'>" + e.Row.Cells[i].Text + "</a>"; } }}//devidefunction.cutStr是截取字符穿方法20代表只显示20个字符,这样就可以做到了//*******************************文字截断加……函数******************************* public static string cutStr(string str, int num) { if ((str.Length) > num) { str = " " + str.Substring(0, num) + "..."; return str; } else { return str; } } 直接写个截取的方法截就行了.6楼的这个方法即可. public static string cutStr(string str, int num) { if ((str.Length) > num) { str = " " + str.Substring(0, num) + "..."; return str; } else { return str; } } 关键那我用的超链接(<a></a>)没有title属性,我怎么获取那个内容啊 //解决方法:数据绑定后过滤每一行即可for (int i = 0; i <= GridView1.Rows.Count - 1; i++) { DataRowView mydrv; string gIntro; if (GridView1.PageIndex == 0) { mydrv = myds.Tables["工作室"].DefaultView[i];//表名 gIntro = Convert.ToString(mydrv["家庭住址"]);//所要处理的字段 GridView1.Rows[i].Cells[3].Text = SubStr(gIntro, 2); } else { mydrv = myds.Tables["工作室"].DefaultView[i + (5 * GridView1.PageIndex)]; gIntro = Convert.ToString(mydrv["家庭住址"]); GridView1.Rows[i].Cells[3].Text = SubStr(gIntro, 2); } } //调用的方法: public string SubStr(string sString, int nLeng) { if (sString.Length <= nLeng) { return sString; } string sNewStr = sString.Substring(0, nLeng); sNewStr = sNewStr + "..."; return sNewStr; } 下面这段代码完全符合楼主要求 <table id="Table1" cellspacing="1" cellpadding="1" width="300" border="1"> <tr> <td width="100"> <div style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis; width: 100px;" title="测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试"> 测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试</div> </td> </tr> </table>显示为: 测试测试测... 我用6楼的这个方法 if ((e.Row.Cells[2].Text != "") || (e.Row.Cells[2].Text != null)) { string str = e.Row.Cells[2].Text.Trim(); str="" 这是为什么? e.Row.Cells[2].Text = cutStr(str, 40); } 对,可以用截取字符串处理,如left函数,只是取标题的前20个汉字,后续的显示为....,但是要说明的是text,ntext字段的标题是不能这么处理的。 http://www.dezai.cn/article_show.asp?ArticleID=15602去看看 另外一种思路:在模板列中将要显示的字段改成TextBox(只读,背景颜色透明,边框去掉),鼠标扫过可显示全部.或再加toolTip属性!! //str 是你的字符串,num截取长度,flg是否带...public static string CutString(string str, int num, bool flg) { #region ASCIIEncoding ascii = new ASCIIEncoding(); int tempLen = 0; string tempString = ""; byte[] s = ascii.GetBytes(str); for (int i = 0; i < s.Length; i++) { if ((int)s[i] == 63) { tempLen += 2; } else { tempLen += 1; } try { tempString += str.Substring(i, 1); } catch { break; } if (tempLen > num) break; } //如果截过则加上半个省略号 byte[] mybyte = System.Text.Encoding.Default.GetBytes(str); if (mybyte.Length > num) if (flg) { tempString += "..."; } return tempString; #endregion } 要吗GridView1_RowDataBound过滤长度要吗用个过滤长度的函数然后使用在模板列中要吗你设置一下那个属性让它自动换行 商品分类表如何设计才合理? 相册的制作 asp.net注册用户名, 密码,手机号码, 邮箱在后台的验证怎么写啊 关于ASP.NET运行机制的问题 十万火急!!! 如何取得应用程序的虚拟目录路径? treeview的背景能设置成透明吗? 关于安装水晶报表环境部署软件cr9netredist_chs.msi时遇到的问题!!!求救 (spy7在吗??) 急!!!一个关于Image控件的ImageUrl绑定问题? [微信问题]获取企业员工USERID问题 datatable如何取多行多列中的某一列某一行的值? 征求一下意见
鼠标移到那显示全部内容——————设置该单元格的title属性为标题内容;
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string sTmp;
string pic;
if (((DataRowView)e.Item.DataItem)["FileTitle"] != DBNull.Value)
{
sTmp = Convert.ToString(((DataRowView)e.Item.DataItem)["FileTitle"]);
pic = Convert.ToString(((DataRowView)e.Item.DataItem)["IsImage"]);
if (pic == "1")
{
string stmp2 = "";
if (sTmp.Length <= 19)
stmp2 = sTmp + "(图)";
if (sTmp.Length > 19)
stmp2 = sTmp.Substring(0, 18) + "…(图)";
sTmp = stmp2;
}
else
{
if (sTmp.Length > 21)
sTmp = sTmp.Substring(0, 20) + "…";
}
((HyperLink)e.Item.FindControl("hylzcwj")).Text = "˙" + sTmp; }
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
if ((e.Row.Cells[i].Text != "") && (i == 5) && (e.Row.Cells[i].Text != null))
{
str = e.Row.Cells[i].Text;
e.Row.Cells[i].Text = cutStr(e.Row.Cells[i].Text, 20);
e.Row.Cells[i].Text = "<a style='cursor:hand;' title='" + str + "'>" + e.Row.Cells[i].Text + "</a>";
}
}
}
//devidefunction.cutStr是截取字符穿方法20代表只显示20个字符,这样就可以做到了
//*******************************文字截断加……函数*******************************
public static string cutStr(string str, int num)
{
if ((str.Length) > num)
{
str = " " + str.Substring(0, num) + "...";
return str;
}
else
{
return str;
}
}
截就行了.6楼的这个方法即可. public static string cutStr(string str, int num)
{
if ((str.Length) > num)
{
str = " " + str.Substring(0, num) + "...";
return str;
}
else
{
return str;
}
}
//解决方法:数据绑定后过滤每一行即可
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
DataRowView mydrv;
string gIntro;
if (GridView1.PageIndex == 0)
{
mydrv = myds.Tables["工作室"].DefaultView[i];//表名
gIntro = Convert.ToString(mydrv["家庭住址"]);//所要处理的字段
GridView1.Rows[i].Cells[3].Text = SubStr(gIntro, 2);
}
else
{
mydrv = myds.Tables["工作室"].DefaultView[i + (5 * GridView1.PageIndex)];
gIntro = Convert.ToString(mydrv["家庭住址"]);
GridView1.Rows[i].Cells[3].Text = SubStr(gIntro, 2);
}
} //调用的方法: public string SubStr(string sString, int nLeng)
{
if (sString.Length <= nLeng)
{
return sString;
}
string sNewStr = sString.Substring(0, nLeng);
sNewStr = sNewStr + "...";
return sNewStr;
}
<tr>
<td width="100">
<div style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis; width: 100px;" title="测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试">
测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试</div>
</td>
</tr>
</table>显示为: 测试测试测...
{
string str = e.Row.Cells[2].Text.Trim(); str="" 这是为什么?
e.Row.Cells[2].Text = cutStr(str, 40);
}
去看看
在模板列中将要显示的字段改成TextBox(只读,背景颜色透明,边框去掉),鼠标扫过可显示全部.
或再加toolTip属性!!
public static string CutString(string str, int num, bool flg)
{
#region
ASCIIEncoding ascii = new ASCIIEncoding();
int tempLen = 0;
string tempString = "";
byte[] s = ascii.GetBytes(str);
for (int i = 0; i < s.Length; i++)
{
if ((int)s[i] == 63)
{
tempLen += 2;
}
else
{
tempLen += 1;
} try
{
tempString += str.Substring(i, 1);
}
catch
{
break;
} if (tempLen > num)
break;
}
//如果截过则加上半个省略号
byte[] mybyte = System.Text.Encoding.Default.GetBytes(str);
if (mybyte.Length > num)
if (flg)
{
tempString += "...";
}
return tempString;
#endregion
}
要吗用个过滤长度的函数然后使用在模板列中
要吗你设置一下那个属性让它自动换行