2010-09-14 85 views
1

我有一個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(); 
} 
+1

請出示這裏的/你是如何結合數據,我懷疑在rowupdating被調用之前,您正在重新綁定數據,因此它只是重複使用舊數據。 – 2010-09-14 19:02:09

+1

你的'dataBind();'函數在做什麼? – 2010-09-14 19:02:35

+0

你做了什麼來解決這個即時通訊有相同的問題 – ONYX 2012-01-05 03:55:11

回答

1

你重新綁定錯誤上回發你的GridView?您應該只從初始加載數據庫中讀取數據:

if (!IsPostBack) 
{ 
    Gridview1.Datasource = BLL.SomeClass.Load(SomeRecordId); 
    GridView1.DataBind(); 
} 
+0

是的,就是這樣。現在正在工作,我將在所需的10分鐘後將其標記爲正確。等待。 – 182764125216 2010-09-14 19:10:05

1

RowUpdating and RowUpdated在不同的時間點火。看看這不是你的問題。

+0

是的,我已經查過了。我最初在昨天錯了,但我已經解決了這個問題。感謝您的建議。 – 182764125216 2010-09-14 19:11:09

1

嘗試使用下面的方法讓您的新價值觀:

//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; 

string Item = e.NewValues["Item"].ToString(); 
string Family = e.NewValues["Family"].ToString(); 
string Structure = e.NewValues["Structure"].ToString(); 
+0

我也試過之前,我的e.NewValues.Count返回0,所以我切換到這種方式,我發現在MSDN庫的例子。 – 182764125216 2010-09-14 19:16:04