I. GridView
...... getData()
{ //调用 1> 2> }
1> 与数据库连接:
1.SqlConnection
2.SqlCommand
3.SqlDataAdapter
4.DataSet
5.da.Fill(ds, table_name);
2> 数据绑定:
this.GridView1.DataSource=ds.Tables[0].DefaultView;
this.GridView1.DataBind();
3> 分页:
. ......... PageIndexChanging(.....)
{ this.GridView1.PageIndex=e.NewPageIndex; //据说这种方法很慢,需要重新获取一遍所有的数据
getData(); }
4> 编辑行:
.......... GridView1_RowEditing(......)
{ this.GridView1.EditIndex=e.NewPageIndex;
getData(); }
5> 取消编辑:
........ GridView1_RowCancelingEidt(......)
{ this.GridView1.EditIndex=-1; getData(); }
6> 更新行:
........ GridView1_RowUpdating(......)
{ 01》 提取控件中的数据 、保存 //在控件属性DataKeyName中添加关键字集合,包括主键
string CustomerID=this.GridView1.DataKeys[e.RowIndex][0].Tostring(); //主键
string CompanyName=((TextBox)this.GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).ToString();
//不能用上述方法,否则提取的仍为原数据库中的值
string ContactName =................................................................Cells[1]......................................;
...............;
02》提交到DB
调用更新函数updateCustomers(CustomerID,CompanyName,ContactName,Address);
03》退出编辑状态
this.GridView1.EditIndex=-1;
04》重新绑定绑定
getData();
}
***更新函数 :
........ updateCustomers(... , ... , ... , ...)
{ SqlConnection cn=new .............;
SqlCommand cmd=new SqlCommand("Update Customers set CompanyName=' "+.........;
cn.Open();
cn.ExecuteNonQuery();
cn.Close();
}
7> 删除行:
............ GridView_RowDeleting(.....)
{ //同上:提取数据、提交到DB【在调用的删除函数中重写相关删除的SQL语句】、退出编辑及重新绑定}
转载于:https://www.cnblogs.com/ycydok/archive/2009/08/17/1547965.html
相关资源:DirectX修复工具V4.0增强版