当前位置:编程文档 >> C# >> 使用GridView实现用“...”代替超长字符串
首页

使用GridView实现用“...”代替超长字符串

所属类别:C#
推荐指数:★★☆
文档人气:4
本周人气:3
发布日期:2008-8-2

    实际需要,所以写了这段使用GridView实现用“...”代替超长字符串的代码,希望给新手一点提示和帮助,大家一起来看代码吧.

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;



public partial class Custom_Control_WebWatch : System.Web.UI.UserControl

{

SqlDB s = new SqlDB();

Upload u = new Upload();

protected void Page_Load(object sender, EventArgs e)

{

if (!Page.IsPostBack)

{

bind();

}

}



public void bind()

{

string sql = "SELECT [Wname], CONVERT(VarChar(10),Wdate,102)as Wdate FROM [Watch] ORDER BY [Wdate] DESC";

DataTable data = s.GetDataSet(sql).Tables[0];

//遍历

for (int i = 0; i < data.Rows.Count; i++)

{

data.Rows[i][0] = ReplaceString(data.Rows[i][0].ToString(), 6);

}

GridView1.DataSource = data;

GridView1.DataBind();

s.Close();

}



private string ReplaceString(string str, int length)

{

if (str.Length > length)

{

str = str.Substring(0, length - 1) + "...";

}

return str;

}

}

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;



public partial class Custom_Control_WebWatch : System.Web.UI.UserControl

{

SqlDB s = new SqlDB();

Upload u = new Upload();

protected void Page_Load(object sender, EventArgs e)

{

if (!Page.IsPostBack)

{

bind();

}

}



public void bind()

{

string sql = "SELECT [Wname], CONVERT(VarChar(10),Wdate,102)as Wdate FROM [Watch] ORDER BY [Wdate] DESC";

DataTable data = s.GetDataSet(sql).Tables[0];

//遍历

for (int i = 0; i < data.Rows.Count; i++)

{

data.Rows[i][0] = ReplaceString(data.Rows[i][0].ToString(), 6);

}

GridView1.DataSource = data;

GridView1.DataBind();

s.Close();

}



private string ReplaceString(string str, int length)

{

if (str.Length > length)

{

str = str.Substring(0, length - 1) + "...";

}

return str;

}

}

文档说明:

     

相关文档


读取评论列表……