做了一个示例,用户更新的数据保存在Session中,点击“更新数据源”按钮后将Session中的数据更新到数据库中,可以在
http://www.webdiyer.com/demo/editdatagrid.aspx 看到演示效果:
文件代码:
test.aspx
===========================>
<%@ Page Language="C#" Debug="True"%>
<%@Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient" %> <script language="C#" runat="server">
const string DataTableName="Employees";
SqlConnection conn;
SqlDataAdapter adapter;
void Page_Load(Object src, EventArgs e)
{
conn=new SqlConnection("server=(local);database=pubs;uid=sa;pwd=");
adapter=new SqlDataAdapter("select * from employees",conn);
if(!Page.IsPostBack){
BindData();
}
} //绑定数据
void BindData(){
//先从Session中获取DataTable
DataTable table=(DataTable)Session[DataTableName];
//若Session中的DataTable不存在,则从数据库获取数据
if(table==null){
table=new DataTable();
adapter.Fill(table);
//将DataTable保存到Session中
SaveTableToSession(table);
table.Columns["id"].AutoIncrement=true;
}
grid.DataSource=table;
grid.DataBind();
} void ChangePage(object src,DataGridPageChangedEventArgs e){
grid.CurrentPageIndex=e.NewPageIndex;
BindData();
} void UpdateDataTable(object src,EventArgs e){
try{
DataTable table=GetTableFromSession();
string name;
byte age;
string address;
CheckBox ckdel;
for(int i=0;i<grid.Items.Count;i++){
DataGridItem dgitem=grid.Items[i];
int empId=(int)grid.DataKeys[dgitem.ItemIndex];
ckdel=dgitem.FindControl("delckb") as CheckBox;
name=((TextBox)dgitem.Cells[0].Controls[1]).Text;
age=byte.Parse(((TextBox)dgitem.Cells[1].Controls[1]).Text);
address=((TextBox)dgitem.Cells[2].Controls[1]).Text;
UpdateEmployee(table,empId,name,age,address,ckdel.Checked);
}
SaveTableToSession(table);
cancelbtn.Enabled=true;
int rowcount=0;
foreach(DataRow row in table.Rows){
if(row.RowState!=DataRowState.Deleted)
rowcount++;
}
if(Math.Ceiling(rowcount/5.0)==grid.CurrentPageIndex&&grid.CurrentPageIndex>0)
grid.CurrentPageIndex-=1;
BindData();
msglbl.Text="更新数据表成功!";
}
catch(Exception ex){
msglbl.Text="更新数据表失败,出现意外错误:"+ex.Message;
}
}
void UpdateEmployee(DataTable table,int id,string name,byte age,string address,bool isDelete){
for(int i=0;i<table.Rows.Count;i++){
DataRow row=table.Rows[i];
//如果选中了删除复选框,就直接就该行数据删除,不用再更新,否则更新该行数据
if(row!=null&&row.RowState!=DataRowState.Deleted){
if((int)row["id"]==id){
if(!isDelete){
row["name"]=name;
row["age"]=age;
row["address"]=address;
}
else
row.Delete();
}
}
}
msglbl.Text="更新数据表成功!";
} void UpdateDataBase(object src,EventArgs e){
try{
DataTable table=GetTableFromSession();
SqlCommandBuilder cmdbd=new SqlCommandBuilder(adapter);
adapter.Update(table);
msglbl.Text="更新数据源成功!";
cancelbtn.Enabled=false;
}
catch(Exception ex){
msglbl.Text="更新数据源失败,出现意外错误:"+ex.Message;
}
BindData();
} void CancelUpdate(object src,EventArgs e){
DataTable table=GetTableFromSession();
table.RejectChanges();
grid.CurrentPageIndex=0;
BindData();
cancelbtn.Enabled=false;
} void AddNewEmployee(object src,DataGridCommandEventArgs e){
if(e.CommandName=="Add"){
try{
DataTable table=GetTableFromSession();
string name=((TextBox)e.Item.FindControl("newname")).Text;
byte age=byte.Parse(((TextBox)e.Item.FindControl("newage")).Text);
string address=((TextBox)e.Item.FindControl("newaddress")).Text;
DataRow row=table.NewRow();
row["name"]=name;
row["age"]=age;
row["address"]=address;
table.Rows.Add(row);
SaveTableToSession(table);
//重新绑定数据
BindData();
msglbl.Text="添加新记录成功!";
cancelbtn.Enabled=true;
}