views:

35

answers:

3

I have a Gridview with these parameters:

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

On updating it calls:

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();        
}

It generates the Update/Edit/Delete buttons, my Delete function is working exactly how I want and the 'Edit' button generates editable TextBoxes as it should.

My problem is in the updating part, the strings Item, Family, Structure are getting the old values, not the new values I put in the generated text boxes.
If I hard code in values they are updated to the database and the DateTime.Now is always updating correctly in the database so the update query is working.

I've been looking at this/reading forums testing things for a couple days now. I'm sure I'm just missing something simple that I have overlooked.

Thanks for any help.

Edit: It has been answered but for those who were curious this is my dataBind();

protected void dataBind()
{
    ItemTableAdapter taItem = new ItemTableAdapter();
    this.ItemGrid.DataSource = taItem.GetActive();
    this.ItemGrid.DataBind();
}
+1  A: 

RowUpdating and RowUpdated fire at different times. See if that isn't your problem.

Brad
Yes, I checked this already. I initially had the wrong one in yesterday but I have already fixed this. Thanks for the advice.
182764125216
+1  A: 

Are you re-binding your GridView on postback by mistake? You should only fetch the data from the database on initial load:

if (!IsPostBack)
{
    Gridview1.Datasource = BLL.SomeClass.Load(SomeRecordId);
    GridView1.DataBind();
}
Larry Flewwelling
Yes this is it. It is now working I will mark it as Correct after the required 10 min. wait.
182764125216
+1  A: 

Try using the following method to get your new values:

//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();
Abe Miessler
I tried that before also, my e.NewValues.Count was returning 0 so I switched to this way I found on the MSDN Library example.
182764125216