我有一个GridView与这些参数:asp.net的GridView不正确更新
<asp:GridView runat="server" ID="ItemGrid" CssClass="Grid"
AutoGenerateColumns="false"
AutoGenerateDeleteButton="true" OnRowDeleting="RowDeleting"
AutoGenerateEditButton="true" onRowEditing="RowEdit"
OnRowCancelingEdit="CancelRowEdit" onRowUpdating="RowUpdating"
DataKeyNames="Item_ID">
<Columns>
<asp:BoundField HeaderText="Item" DataField="Item"/>
<asp:BoundField HeaderText="Family" DataField="Family"/>
<asp:BoundField HeaderText="Structure" DataField="Structure"/>
<asp:BoundField HeaderText="Updated" ReadOnly="true" DataFormatString="{0:d}" DataField="Updated"/>
</Columns>
</asp:GridView>
在更新它调用:
protected void RowUpdating(object sender, GridViewUpdateEventArgs e){
int Item_ID = (int)this.ItemGrid.DataKeys[e.RowIndex][0];
//Problem is something right here:
string Item = ((TextBox)ItemGrid.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
string Family = ((TextBox)ItemGrid.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
string Structure = ((TextBox)ItemGrid.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
ItemTableAdapter taItem = new ItemTableAdapter();
taItem.UpdateItem(Item, Family, Structure, DateTime.Now, Item_ID);
//just a <asp:Label> for seeing some output.
Alert.Text= string.Format("Item:{0}Family:{1}Structure:{2}",Item,Family,Structure);
this.ItemGrid.EditIndex = -1;
dataBind();
}
它生成更新/编辑/删除按钮,我删除功能正在按照我想要的方式工作,并且“编辑”按钮生成可编辑的文本框。
我的问题是在更新部件,字符串项,家庭,结构越来越老的值,而不是我把生成的文本框中输入新值。
如果我在值硬编码它们更新到数据库和DateTime.Now
总是在数据库中正确更新,因此更新查询工作。
我一直在寻找这个/阅读论坛测试事情几天了。我确信我只是错过了一些我忽略的东西。
感谢您的任何帮助。
编辑: 它已被回答,但对于那些好奇这是我的dataBind();
protected void dataBind()
{
ItemTableAdapter taItem = new ItemTableAdapter();
this.ItemGrid.DataSource = taItem.GetActive();
this.ItemGrid.DataBind();
}
请出示这里的/你是如何结合数据,我怀疑在rowupdating被调用之前,您正在重新绑定数据,因此它只是重复使用旧数据。 – 2010-09-14 19:02:09
你的'dataBind();'函数在做什么? – 2010-09-14 19:02:35
你做了什么来解决这个即时通讯有相同的问题 – ONYX 2012-01-05 03:55:11