asp.net 2.0下一个标准GRIDVIEW功能的实现(不用datasource控件)(3) { ViewState["sortdirection"] = "desc"; } else { ViewState["sortdirection"] = "asc"; } } BindGrid(); }很明显,设置viewsate来保存每次排序时的顺序,上面的相信很容易理解。 最后,实现编辑功能,因为在aspx页面中已经设置了OnRowEditing="GridView1_RowEditing",其中GridView1_RowEditing的代码为 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { int empid; string fname, lname; empid = int.Parse(GridView1.Rows[e.RowIndex].Cells[0].Text); fname = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text; lname = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
SqlConnection cnn = new SqlConnection(@"data source=localhost;initial catalog=northwind;user id=sa;passWord=123456"); cnn.Open(); SqlCommand cmd = new SqlCommand("update employees set firstname=@fname,lastname=@lname where employeeid=@empid", cnn);