工作中点滴记录

永远保持学徒心态

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

后台代码:

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 }
posted on 2011-02-25 15:55  梦里故乡  阅读(286)  评论(0编辑  收藏  举报