GridView中的行更新 [英] row updating in gridview
问题描述
嗨......
我开发了一个带有gridview控件和一个xml文件的应用程序.
我将xml数据绑定到gridview.它工作正常.但是在编辑时,它显示了这样的错误
无法将类型为"System.Data.DataTable"的对象转换为类型为"System.Data.DataSet".
我的设计是:
hi....
I developed an application with gridview control and one xml file.
I bind xml data to gridview.Its working fine.But at the time of editing it shows an error like this
Unable to cast object of type ''System.Data.DataTable'' to type ''System.Data.DataSet''.
My design is:
<asp:GridView ID="grdv" runat="server" AutoGenerateColumns="False" AutoGenerateEditButton="True" Height="67px" Width="214px" onrowcancelingedit="grdv_RowCancelingEdit" onrowediting="grdv_RowEditing" onrowupdating="grdv_RowUpdating" >
<columns>
<asp:TemplateField HeaderText="CompanyName">
<itemtemplate>
<%# Eval("CompanyName") %>
</itemtemplate>
<edititemtemplate>
<asp:TextBox ID="txtJobTitle1" runat="Server" Text='<%#Eval("CompanyName")%>'Columns="30">
</edititemtemplate>
<asp:TemplateField HeaderText="Description">
<itemtemplate>
<%# Eval("Description")%>
</itemtemplate>
<edititemtemplate>
<asp:TextBox ID="txtDescription1" runat="Server" Text='<%# Eval("Description")%>'Columns="30">
</edititemtemplate>
</columns>
我的CS文件是:
My CS flle is:
public partial class Admin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//txtJobTitle.Focus();
//txtDescription.Focus();
GetData();
}
protected void grdv_RowEditing(object sender, GridViewEditEventArgs e)
{
grdv.EditIndex = e.NewEditIndex;
GetData();
}
protected void grdv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//GridViewRow row = (GridViewRow)grdv.Rows[e.RowIndex];
int i = grdv.Rows[e.RowIndex].DataItemIndex;
//TextBox txtName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtName");
TextBox CompanyName = ((TextBox)grdv.Rows[e.RowIndex].FindControl("txtJobTitle1"));
TextBox Description = ((TextBox)grdv.Rows[e.RowIndex].FindControl("txtDescription1"));
//int i = grdv.Rows[e.RowIndex].DataItemIndex;
//string CompanyName = ((TextBox)grdv.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
//string Description = ((TextBox)grdv.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
grdv.EditIndex = -1;
GetData();
DataSet dss =(DataSet)grdv.DataSource;
dss.Tables[0].Rows[i]["CompanyName"] = CompanyName;
dss.Tables[0].Rows[i]["Description"] = Description;
dss.WriteXml(Server.MapPath("XMLJobFile.xml"));
GetData();
}
protected void grdv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grdv.EditIndex = -1;
GetData();
}
protected void GetData()
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("XMLJobFile.xml"));
grdv.DataSource = ds.Tables[0];
grdv.DataBind();
}
}
我的Xml文件是:
And my Xml file is:
<<pre lang="xml">?xml version="1.0" standalone="yes"?>
<JOBS>
<Fresher>
<CompanyName>
Rayars Group: </CompanyName>
<Description>
We are looking for Graduates (B.sc/B.com and other non-engineering streams) who have decent knowledge about webdesigning
</Description>
</Fresher>
</JOBS>
必须用新值更新该行,并且更改将反映到xml文件中.
请提供任何解决方案
have to update the row with new value and the changes are reflected to xml file.
Please give any solution
推荐答案
DataSet dss =(DataSet)grdv.DataSource;
---问题专线
做这个
---Problematic Line
Do This
DataTable dt = (DataTable)gridSlips.DataSource; ;
DataSet dss=new DataSet();
dss.Tables[0]=dt ;
dss.Tables[0].Rows[i]["CompanyName"] = CompanyName;
dss.Tables[0].Rows[i]["Description"] = Description;
dss.WriteXml(Server.MapPath("XMLJobFile.xml"));
GetData();
Swati很抱歉,实际上这条线未经测试,只能通过逻辑完成
现在使用
Swati Sorry For That Line Actually That was untested and Done By logic Only
Now use
DataTable dt = (DataTable)gridSlips.DataSource; ;
DataSet dss=new DataSet();
dss.Tables.Remove();
dss.Tables.Add(dt);
dss.Tables[0].Rows[i]["CompanyName"] = CompanyName;
dss.Tables[0].Rows[i]["Description"] = Description;
dss.WriteXml(Server.MapPath("XMLJobFile.xml"));
GetData();
我已经测试并实现了全部
This i have Tested and Implemented This All
这篇关于GridView中的行更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!