为什么在尝试通过Gridview更新/删除一行时,XML文件中的所有数据都被删除了
本文关键字:删除 文件 XML 数据 一行 Gridview 更新 为什么 | 更新日期: 2023-09-27 18:26:43
我有一个可编辑网格视图,它的功能来自XML文件。当我尝试通过Gridiview更新/删除一行时,XML文件中的所有数据都会被删除。插入新行时,挂起的数据将替换为新行。我能告诉你我做错了什么吗?我提供了我的xml文件和用于编辑GridView的四种方法。
XML:
<root>
<pos>
<partNumbers>
<partid>0</partid>
<partnumber>796542</partnumber>
</partNumbers>
<partNumbers>
<partid>1</partid>
<partnumber>225614</partnumber>
</partNumbers>
<partNumbers>
<partid>2</partid>
<partnumber>123457</partnumber>
</partNumbers>
</pos>
</root>
源代码-C#-:
//Bind Data
protected void BindGridView()
{
DataSet dsgvPartNumber = new DataSet();
dsgvPartNumber.ReadXml(Server.MapPath("~/xml/storeUserInfo.xml"));
gvPartNumber.DataSource = dsgvPartNumber.Tables["partNumbers"];
gvPartNumber.DataBind();
gvPartNumber.ShowFooter = true;
}
//Delete
protected void gvPartNumber_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
BindGridView();
DataTable dsgvPartNumberDelete = (DataTable)gvPartNumber.DataSource;
dsgvPartNumberDelete.Rows[gvPartNumber.Rows[e.RowIndex].DataItemIndex].Delete();
dsgvPartNumberDelete.WriteXml(Server.MapPath("~/xml/storeUserInfo.xml"));
BindGridView();
}
//Update GridView
protected void gvPartNumber_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int index = gvPartNumber.Rows[e.RowIndex].DataItemIndex;
string partId = ((TextBox)gvPartNumber.Rows[e.RowIndex].FindControl("txtPartID")).Text;
string partNumber = ((TextBox)gvPartNumber.Rows[e.RowIndex].FindControl("txtPartNumber")).Text;
gvPartNumber.EditIndex = -1;
BindGridView();
DataTable dtUpdateXMLFile = (DataTable)gvPartNumber.DataSource;
dtUpdateXMLFile.Rows[index]["partid"] = partId;
dtUpdateXMLFile.Rows[index]["partnumber"] = partNumber;
dtUpdateXMLFile.WriteXml(Server.MapPath("~/xml/storeUserInfo.xml"));
BindGridView();
}
//Insert New Row
protected void gvPartNumber_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "insertXMLData")
{
string partid = ((TextBox)gvPartNumber.FooterRow.FindControl("txtPartIDInsert")).Text;
string partnumber = ((TextBox)gvPartNumber.FooterRow.FindControl("txtPartNumberInsert")).Text;
BindGridView();
DataTable dtXMLInsert = (DataTable)gvPartNumber.DataSource;
DataRow drInsert = dtXMLInsert.NewRow();
drInsert["partid"] = partid;
drInsert["partnumber"] = partnumber;
dtXMLInsert.Rows.Add(drInsert);
dtXMLInsert.WriteXml(Server.MapPath("~/xml/storeUserInfo.xml"));
BindGridView();
}
}
我可以看到你正在将数据表保存到错误的xml文件中:
DataTable dtUpdateXMLFile = (DataTable)gvPartNumber.DataSource;
dtUpdateXMLFile.Rows[index]["partid"] = partId;
dtUpdateXMLFile.Rows[index]["partnumber"] = partNumber;
dtUpdateXMLFile.WriteXml(Server.MapPath("~/xml/storeUserInfo.xml"));
记住,这个表只是来自xml文件的一部分(还有另一个表,pos)。实际上,你必须保存数据集,而不是这个表。首先将表更新到数据集,然后保存它。您的方法可能看起来像:
//Delete
protected void gvPartNumber_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
BindGridView();
DataSet dsgvPartNumber = new DataSet();
dsgvPartNumber.ReadXml(Server.MapPath("~/xml/storeUserInfo.xml"));
dsgvPartNumber.Tables["partNumbers"].Rows[gvPartNumber.Rows[e.RowIndex].DataItemIndex].Delete();
dsgvPartNumber.WriteXml(Server.MapPath("~/xml/storeUserInfo.xml"));
BindGridView();
}
//Update GridView
protected void gvPartNumber_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int index = gvPartNumber.Rows[e.RowIndex].DataItemIndex;
string partId = ((TextBox)gvPartNumber.Rows[e.RowIndex].FindControl("txtPartID")).Text;
string partNumber = ((TextBox)gvPartNumber.Rows[e.RowIndex].FindControl("txtPartNumber")).Text;
gvPartNumber.EditIndex = -1;
BindGridView();
DataSet dsgvPartNumber = new DataSet();
dsgvPartNumber.ReadXml(Server.MapPath("~/xml/storeUserInfo.xml"));
dsgvPartNumber.Tables["partNumbers"].Rows[index]["partid"] = partId;
dsgvPartNumber.Tables["partNumbers"].Rows[index]["partnumber"] = partNumber;
dsgvPartNumber.WriteXml(Server.MapPath("~/xml/storeUserInfo.xml"));
BindGridView();
}
//Insert New Row
protected void gvPartNumber_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "insertXMLData")
{
string partid = ((TextBox)gvPartNumber.FooterRow.FindControl("txtPartIDInsert")).Text;
string partnumber = ((TextBox)gvPartNumber.FooterRow.FindControl("txtPartNumberInsert")).Text;
BindGridView();
DataTable dtXMLInsert = (DataTable)gvPartNumber.DataSource;
DataSet dsgvPartNumber = new DataSet();
dsgvPartNumber.ReadXml(Server.MapPath("~/xml/storeUserInfo.xml"));
DataRow drInsert = dsgvPartNumber.Tables["partNumbers"].NewRow();
drInsert["partid"] = partid;
drInsert["partnumber"] = partnumber;
dsgvPartNumber.Tables["partNumbers"].Rows.Add(drInsert);
dsgvPartNumber.WriteXml(Server.MapPath("~/xml/storeUserInfo.xml"));
BindGridView();
}
}