我有一個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