2010-09-14 88 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