Gridview的中数据的截取(2)

后台代码:

View Code
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 DataView view = ds.Tables[0].DefaultView;
22 for (int i = 0; i < this.GridView1.Rows.Count; i++)
23 {
24 if (this.GridView1.PageIndex == 0)
25 {
26 DataRowView rowView = view[i];
27 string str = rowView["CompanyName"].ToString();
28 this.GridView1.Rows[i].Cells[1].Text = SubstringStr(str, 4);
29 }
30 else
31 {
32 DataRowView rowView = view[i + (this.GridView1.PageIndex * this.GridView1.PageSize)];
33 string str = rowView["CompanyName"].ToString();
34 this.GridView1.Rows[i].Cells[1].Text = SubstringStr(str, 4);
35 }
36
37 }
38 }
39  public string SubstringStr(string str, int len)
40 {
41 if (str.Length <= len)
42 {
43 return str;
44 }
45 else
46 {
47 return str.Substring(0, len) + "...";
48 }
49 }
怀揣着一点点梦想的年轻人
相信技术和创新的力量
喜欢快速反应的工作节奏
原文地址:https://www.cnblogs.com/hfliyi/p/1964970.html