怎么样在gridview的字段显示限定在特定的字数内 如题,我想把一些内容的标题显示出来的话,我想达到整齐的效果,要把那些标题的字数限定,请如如何实现呢,谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 /// <summary> /// 截取字符串函数 /// </summary> /// <param name="str">所要截取的字符串</param> /// <param name="num">截取字符串的长度</param> /// <returns></returns> static public string GetSubString(string str, int num) { #region return (str.Length > num) ? str.Substring(0, num) + "......" : str; #endregion }这就ok了 /// <summary> /// 截取字符串函数 /// </summary> /// <param name="str">所要截取的字符串 </param> /// <param name="num">截取字符串的长度 </param> /// <returns> </returns> 这些是写在哪里的呢,这样写吗 <summary> GetSubString() </summary> <param name="str">dt["家庭住址"].ToString()</param> <param name="num">20</param> <returns> </returns> static public string GetSubString(string str, int num) { #region return (str.Length > num) ? str.Substring(0, num) + "......" : str; #endregion } 不好意思,我真的不会,我就是想让家庭住址的字符限定在10个字内,其它用省略号表示,能不能帮我写一个完整的例子,谢谢了 很简单的其实,在Grid View的DataBound事件里用Substring(0,10)处理一下就可以了,如果你是自己写代码绑定的Grid View就在绑定前把DataSet里的指定列foreach调用Substring(0,10)后,再绑定就可以了,我还是觉得第一种简单些。protected void GridView1_DataBound(object sender, EventArgs e) { for (int i = 0; i <= GridView1.Rows.Count - 1; i++) { if (GridView1.Rows[i].Cells[2].Text.Length > 10)//Cells[2]是你要限定字符的列数 { GridView1.Rows[i].Cells[2].Text = GridView1.Rows[i].Cells[2].Text.Substring(0, 10) + "..."; } } } 在行绑定事件中直接处理:protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { // 假设列Cells[1]是家庭住址 if (e.Row.Cells[1].Text.Trim().Length > 10) { e.Row.Cells[1].Text = e.Row.Cells[1].Text.Trim().Substring(0, 10) + "......"; } } } 怎么还是不行的,我的就只有一列的,第一列是不是Cell[0]呀,这个0应该是指第一列吧,照5楼那样 protected void GridView2_DataBound(object sender, EventArgs e) { for (int i = 0; i <= GridView2.Rows.Count - 1; i++) { if (GridView2.Rows[i].Cells[0].Text.Length > 10)//Cells[2]是你要限定字符的列数 { GridView2.Rows[i].Cells[0].Text = GridView2.Rows[i].Cells[0].Text.Substring(0, 10) + "..."; } } } 不行呀六楼这样也不行protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { if (e.Row.Cells[0].Text.Trim().Length > 10) { e.Row.Cells[0].Text = e.Row.Cells[0].Text.Trim().Substring(0, 10) + "......"; } } } 上面写错了,是GridView2的,我写错了,还是不行 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { for (int i == 0;i<e.Row.Count -1;i++) { e.Row.Cells[0].Text = e.Row.Cells[0].Text.Trim().Substring(0, 10) + "......"; } } 还是不行的,我的代码如下:public partial class article : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { string mycon = ConfigurationSettings.AppSettings["mc"]; SqlConnection con = new SqlConnection(mycon); SqlDataAdapter mycom=new SqlDataAdapter("select top 5 * from bjb",con); DataSet ds=new DataSet(); mycom.Fill(ds,"bjb"); GridView1.DataSource=new DataView(ds.Tables[0]); GridView1.DataBind(); SqlDataAdapter com = new SqlDataAdapter("select top 10 * from student", con); DataSet dt = new DataSet(); com.Fill(dt, "student"); GridView2.DataSource = new DataView(dt.Tables[0]); GridView2.DataBind(); } protected void GridView2_DataBound(object sender, EventArgs e) { for (int i = 0; i <= GridView2.Rows.Count - 1; i++) { if (GridView2.Rows[i].Cells[0].Text.Length > 10) { GridView2.Rows[i].Cells[0].Text = GridView2.Rows[i].Cells[0].Text.Substring(0, 10) + "..."; } } } 可是运行结果如下:家庭住址 您选择了家庭住址是广东省佛山市顺德 的学生 您选择了家庭住址是广东省梅州市丰顺县 的学生 您选择了家庭住址是广东省河源市紫金县 的学生 您选择了家庭住址是广东省揭阳市惠来县 的学生 您选择了家庭住址是广东省珠海市 的学生 您选择了家庭住址是广东省梅州市大埔县 的学生 您选择了家庭住址是广东省连州市 的学生 您选择了家庭住址是广东省广州市 的学生 您选择了家庭住址是广东省广州市 的学生 您选择了家庭住址是广东省汕头市金平区 的学生 用这个吧:http://download.csdn.net/source/303583 aspx与aspx.cs之间怎么传值啊? 如何将一个字符串的前面几个字符删除? 谁能提供Session超时的实例啊!急需一个完整的实例! 再一贴:中英文网站问题~~~ asp.net(C#)怎样取得存储过程执行结果集? 声卡驱动安装问题(在计算机技术版块发了,没人理,只能在这里发了!) 学习.net遇到天花板了,怎么办啊? asp.net 时间比较应该怎么写? 一个非常简单的问题 求助 asp.net 远程访问服务器 问题 急,赶毕业设计~~~~~gridview点击删除提示框确认后无反映? .net项目问题
/// 截取字符串函数
/// </summary>
/// <param name="str">所要截取的字符串</param>
/// <param name="num">截取字符串的长度</param>
/// <returns></returns>
static public string GetSubString(string str, int num)
{
#region
return (str.Length > num) ? str.Substring(0, num) + "......" : str;
#endregion
}
这就ok了
/// 截取字符串函数
/// </summary>
/// <param name="str">所要截取的字符串 </param>
/// <param name="num">截取字符串的长度 </param>
/// <returns> </returns>
这些是写在哪里的呢,这样写吗
<summary>
GetSubString()
</summary>
<param name="str">dt["家庭住址"].ToString()</param>
<param name="num">20</param>
<returns> </returns>
static public string GetSubString(string str, int num)
{
#region
return (str.Length > num) ? str.Substring(0, num) + "......" : str;
#endregion
}
不好意思,我真的不会,我就是想让家庭住址的字符限定在10个字内,其它用省略号表示,能不能帮我写一个完整的例子,谢谢了
如果你是自己写代码绑定的Grid View就在绑定前把DataSet里的指定列foreach调用Substring(0,10)后,
再绑定就可以了,我还是觉得第一种简单些。protected void GridView1_DataBound(object sender, EventArgs e)
{
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
if (GridView1.Rows[i].Cells[2].Text.Length > 10)//Cells[2]是你要限定字符的列数
{
GridView1.Rows[i].Cells[2].Text = GridView1.Rows[i].Cells[2].Text.Substring(0, 10) + "...";
}
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// 假设列Cells[1]是家庭住址
if (e.Row.Cells[1].Text.Trim().Length > 10)
{
e.Row.Cells[1].Text = e.Row.Cells[1].Text.Trim().Substring(0, 10) + "......";
}
}
}
protected void GridView2_DataBound(object sender, EventArgs e)
{
for (int i = 0; i <= GridView2.Rows.Count - 1; i++)
{
if (GridView2.Rows[i].Cells[0].Text.Length > 10)//Cells[2]是你要限定字符的列数
{
GridView2.Rows[i].Cells[0].Text = GridView2.Rows[i].Cells[0].Text.Substring(0, 10) + "...";
}
}
} 不行呀六楼这样也不行
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[0].Text.Trim().Length > 10)
{
e.Row.Cells[0].Text = e.Row.Cells[0].Text.Trim().Substring(0, 10) + "......";
}
}
}
{
for (int i == 0;i<e.Row.Count -1;i++)
{
e.Row.Cells[0].Text = e.Row.Cells[0].Text.Trim().Substring(0, 10) + "......";
}
}
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
string mycon = ConfigurationSettings.AppSettings["mc"];
SqlConnection con = new SqlConnection(mycon);
SqlDataAdapter mycom=new SqlDataAdapter("select top 5 * from bjb",con);
DataSet ds=new DataSet();
mycom.Fill(ds,"bjb");
GridView1.DataSource=new DataView(ds.Tables[0]);
GridView1.DataBind();
SqlDataAdapter com = new SqlDataAdapter("select top 10 * from student", con);
DataSet dt = new DataSet();
com.Fill(dt, "student");
GridView2.DataSource = new DataView(dt.Tables[0]);
GridView2.DataBind();
}
protected void GridView2_DataBound(object sender, EventArgs e)
{
for (int i = 0; i <= GridView2.Rows.Count - 1; i++)
{
if (GridView2.Rows[i].Cells[0].Text.Length > 10)
{
GridView2.Rows[i].Cells[0].Text = GridView2.Rows[i].Cells[0].Text.Substring(0, 10) + "...";
}
}
}
可是运行结果如下:
家庭住址
您选择了家庭住址是广东省佛山市顺德 的学生
您选择了家庭住址是广东省梅州市丰顺县 的学生
您选择了家庭住址是广东省河源市紫金县 的学生
您选择了家庭住址是广东省揭阳市惠来县 的学生
您选择了家庭住址是广东省珠海市 的学生
您选择了家庭住址是广东省梅州市大埔县 的学生
您选择了家庭住址是广东省连州市 的学生
您选择了家庭住址是广东省广州市 的学生
您选择了家庭住址是广东省广州市 的学生
您选择了家庭住址是广东省汕头市金平区 的学生