Gridview的中数据的截取

后端代码:

后端代码
1 protected void Page_Load(object sender, EventArgs e)
2 {
3 if (!IsPostBack)
4 {
5 databind();
6 }
7
8 }
9 public void databind()
10 {
11 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ToString());
12 SqlCommand cmd = new SqlCommand();
13 cmd.Connection = con;
14 cmd.CommandText = "Select * From Customers";
15 SqlDataAdapter da = new SqlDataAdapter(cmd);
16 DataSet ds = new DataSet();
17 da.Fill(ds);
18 this.GridView1.DataSource = ds.Tables[0];
19 this.GridView1.DataKeyNames = new string[] { "CustomerID" };
20 this.GridView1.DataBind();
21 }
22 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
23 {
24 if (e.Row.RowType == DataControlRowType.DataRow)
25 {
26 e.Row.Cells[1].Text = SubstringStr(e.Row.Cells[1].Text, 5);
27 }
28 }
29 public string SubstringStr(string str, int len)
30 {
31 if (str.Length <= len)
32 {
33 return str;
34 }
35 else
36 {
37 return str.Substring(0, len) + "...";
38 }
39 }
怀揣着一点点梦想的年轻人
相信技术和创新的力量
喜欢快速反应的工作节奏
原文地址:https://www.cnblogs.com/hfliyi/p/1964966.html