我有一个可编辑的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();
}
}
我可以请一些方向/解释我在代码中缺少的内容吗?
提前谢谢。
答案 0 :(得分:4)
这是因为您将DataSource
设置为DataTable
:
gvPartNumber.DataSource = dsgvPartNumber.Tables["partNumbers"];
不是DataSet
。所以,而不是:
DataSet dsgvPartNumberDelete = (DataSet)gvPartNumber.DataSource;
这样做:
DataTable dsgvPartNumberDelete = (DataTable)gvPartNumber.DataSource;
然后在以下几行中,您将不需要像.Tables[0]
这样的访问器。它已经是DataTable
,因此无论如何都更直接。
当然,您必须将此修改复制到您尝试将DataSource
转换为DataSet
的所有方法。
答案 1 :(得分:1)
您的数据源是DataTable,而不是DataSet。您无法将DataTable转换为DataSet。
DataTable dtUpdateXMLFile = (DataTable)gvPartNumber.DataSource;
dtUpdateXMLFile.Rows[index]["partid"] = partId;
dtUpdateXMLFile.Rows[index]["partnumber"] = partNumber;
dtUpdateXMLFile.WriteXml(Server.MapPath("~/xml/storeUserInfo.xml"));