//绑定修改前的XML OldData.DataSource = myDs.Tables[0].DefaultView; OldData.DataBind(); //当DS当中增加新行 DataRow newRow = myDs.Tables[0].NewRow(); newRow["id"] = "44"; newRow["userName"] = "test user"; newRow["userPass"] = "test passwd"; myDs.Tables[0].Rows.Add(newRow); //绑定新增一条记录的数据 NewData.DataSource = myDs.Tables[0].DefaultView; NewData.DataBind(); //进行过滤 DataRow[] editRow = myDs.Tables[0].Select("id = '44'"); for(int i=0;i<editRow.Length;i++) { editRow[i]["userName"] = "changed user"; editRow[i]["userPass"] = "changed passwd"; } //绑定修改记录后的数据 EditData.DataSource = myDs.Tables[0].DefaultView; EditData.DataBind(); for(int i=0;i<editRow.Length;i++) { editRow[i].Delete(); //把该数据删除 } //绑定删除记录后的数据 DeleData.DataSource = myDs.Tables[0].DefaultView; DeleData.DataBind(); //将myDs的改变写入XML try { myDs.WriteXml(Server.MapPath("./files/"+fileName), XmlWriteMode.WriteSchema); } catch (Exception ex) { Response.Write(ex.Message); }
myDs.Dispose(); } </script> <html> <head> <title>编辑数据</title> </head> <body> <form runat="server"> <table width="100%"> <tr> <td>原数据</td> <td>新增行</td> <td>修改行</td> <td>删除行</td> </tr> <tr> <td valign="top"><asp:DataGrid id="OldData" runat="server"/></td> <td valign="top"><asp:DataGrid id="NewData" runat="server"/></td> <td valign="top"><asp:DataGrid id="EditData" runat="server"/></td> <td valign="top"><asp:DataGrid id="DeleData" runat="server"/></td> </tr> </table> </form> </body> </html>
|