GridView中关于增改数据在临时表的问题

测试界面

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.UI;
 6 using System.Web.UI.WebControls;
 7 using System.Data;
 8 using StudentInfoSys.Dao;
 9 
10 namespace StudentInfoSys
11 {
12     public partial class Text : System.Web.UI.Page
13     {
14         public static DataTable dt;
15         protected void Page_Load(object sender, EventArgs e)
16         {
17             if (!IsPostBack)
18             {
19                 dt = StudentDao.getRoleName().Tables[0];
20                 DataGridViewBind();
21             }
22         }
23         public void DataGridViewBind()
24         {
25             GridView1.DataSource = dt;
26             GridView1.DataBind();
27         }
28         protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
29         {
30             //遍历所有行设置边框样式
31             foreach (TableCell tc in e.Row.Cells)
32             {
33                 tc.Attributes["style"] = "border-color:Black";
34             }
35             //用索引来取得编号
36             if (e.Row.RowIndex != -1)
37             {
38                 int id = e.Row.RowIndex + 1;
39                 e.Row.Cells[0].Text = id.ToString();
40             }
41             //判定当前的行是否属于datarow类型的行
42             if (e.Row.RowType == DataControlRowType.DataRow)
43             {
44                 //当鼠标放上去的时候 先保存当前行的背景颜色并给附一颜色
45                 e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='Azure',this.style.fontWeight='';");
46                 //当鼠标离开的时候 将背景颜色还原的以前的颜色
47                 e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
48             }
49         }
50         protected void Add_Click(object sender, EventArgs e)
51         {
52             int index = dt.Rows.Count - 1;
53             DataRow dr1 = dt.Rows[index];
54             if (dr1[0].ToString() == "")
55             {
56                 dr1.Delete();
57             }
58             DataRow dr = dt.NewRow();
59             dt.Rows.Add(dr);
60             GridView1.EditIndex = dt.Rows.Count - 1;
61             DataGridViewBind();
62         }
63         protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
64         {
65             int index = e.RowIndex;
66             string roleName = ((TextBox)(GridView1.Rows[GridView1.EditIndex].Cells[1].Controls[0])).Text.Trim();
67             DataRow dr = dt.Rows[index];
68             dr[0] = roleName;
69             GridView1.EditIndex = -1;
70             DataGridViewBind();
71         }
72         protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
73         {
74             int index = e.RowIndex;
75             DataRow dr = dt.Rows[index];
76             if (dr[0].ToString() == "")
77             {
78                 dr.Delete();
79             }
80             GridView1.EditIndex = -1;
81             DataGridViewBind();
82         }
83         protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
84         {
85             GridView1.EditIndex = e.NewEditIndex;
86             DataGridViewBind();
87         }
88     }
89 }
View Code

主要用于个人收藏关于datatable的增删改,以及变量用static属性解决页面生命周期被重新定义的问题。

posted @ 2013-08-01 15:41  慕寒  阅读(485)  评论(0)    收藏  举报