Computer
Computer

Reputation: 2227

Gridview data not refreshing

I have a gridview which has a List (Of MyObject) as its datasource.

gv.DataSource = MyListObject
gv.Databind()

Under a templatefield i have a button configured to delete a record from MyListObject and then rebind it. To add a record i have the below code

Protected Sub btnAddRecord_Click(sender As Object, e As EventArgs) Handles btnAddRecord.Click
    Dim Customer As New Customer

    With Customer
        .Name = txtName.Text
        .Surname = txtSurname.Text
        .....
        .ID += MyListObject.Count
    End With

    MyListObject.Add(Customer)

    gv.DataSource = MyListObject 
    gv.DataBind()
End Sub

This works fine, but then i need to allow the user to delete a record if need be:

Private Sub gv_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv.RowCommand
    If e.CommandName = "Delete" Then
        MyListObject.RemoveAt(e.CommandArgument)

        gv.DataSource = Nothing
        gv.DataBind()

        gv.DataSource = MyObjectList
        gv.DataBind()

        upnl.UpdateMode = UpdatePanelUpdateMode.Conditional
        upnl.Update()
    End If
End Sub

When i click the button it deletes the record but doesnt refresh the data. By that i mean when the record is added i am assigning the ID as a row ID and then use that ID to remove the record. Since the List and Gridview values are now out of sync i set the datasource to nothing and rebind it in order that i was going to have the values reset and the ID would be the correct one - but this doesnt works as i expected.

Could anyone advise where im going wrong and how to correct this problem?

Upvotes: 0

Views: 3565

Answers (3)

Computer
Computer

Reputation: 2227

I added CommandArgument='<%# Container.DataItemIndex %>' which resolved the issue as i was then deleting the row the user clicked against.

Upvotes: 1

infocyde
infocyde

Reputation: 4171

Just as a side note, I'd probably handle what get's updated when on the client side using the ajax script manager rather than doing it in the code behind. Saves headaches. The above might be updating the update panel correctly, but the ajax plumbing may not be there on the client side.

Upvotes: 0

gmail user
gmail user

Reputation: 2783

Is gridview in the updatepanel? If yes, that panel should also be refreshed.

Upvotes: 1

Related Questions