0

我有一个可编辑的 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();
        }
}

我可以就我在代码中遗漏的内容获得一些方向/解释吗?

先感谢您。

4

2 回答 2

4

这是因为您将其设置DataSource为 a DataTable

gvPartNumber.DataSource = dsgvPartNumber.Tables["partNumbers"];

不是一个DataSet。所以,而不是这个:

DataSet dsgvPartNumberDelete = (DataSet)gvPartNumber.DataSource;

做这个:

DataTable dsgvPartNumberDelete = (DataTable)gvPartNumber.DataSource;

然后在以下几行中,您将不需要像.Tables[0]. 无论如何,它已经是一个DataTable并且因此更直接的访问。

当然,必须将这种修改复制到您尝试将DataSourceas 转换为的所有方法中DataSet

于 2013-08-06T16:21:12.567 回答
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"));
于 2013-08-06T16:21:14.527 回答