我有一个可编辑的 Gridview,它的数据存储在一个 XML 文件中,当我尝试向 Gridview 插入/更新/删除数据时,我收到以下错误:
无法将“System.Data.DataTable”类型的对象转换为“System.Data.DataSet”类型
Gridview 成功显示了存储在 xml 文件中的数据,但是当我尝试插入/更新/删除行时出错。
我可以就我在这里缺少的东西获得一些指导吗?
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 Row
protected void gvPartNumber_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
BindGridView();
DataSet dsgvPartNumberDelete = (DataSet)gvPartNumber.DataSource;
dsgvPartNumberDelete.Tables[0].Rows[gvPartNumber.Rows[e.RowIndex].DataItemIndex].Delete();
dsgvPartNumberDelete.WriteXml(Server.MapPath("~/xml/storeUserInfo.xml"));
BindGridView();
}
// EditGridView
protected void gvPartNumber_RowEditing(object sender, GridViewEditEventArgs e)
{
gvPartNumber.ShowFooter = false;
gvPartNumber.EditIndex = e.NewEditIndex;
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 dsUpdateXMLFile = (DataSet)gvPartNumber.DataSource;
dsUpdateXMLFile.Tables[0].Rows[index]["partid"] = partId;
dsUpdateXMLFile.Tables[0].Rows[index]["partnumber"] = partNumber;
dsUpdateXMLFile.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();
DataSet dsXMLInsert = (DataSet)gvPartNumber.DataSource;
DataRow drInsert = dsXMLInsert.Tables[0].NewRow();
drInsert["partid"] = partid;
drInsert["partnumber"] = partnumber;
dsXMLInsert.Tables[0].Rows.Add(drInsert);
dsXMLInsert.WriteXml(Server.MapPath("~/xml/storeUserInfo.xml"));
BindGridView();
}
}
我可以就我在代码中遗漏的内容获得一些方向/解释吗?
先感谢您。