GridView not refreshing even after DataBind

Last post 07-06-2009 3:33 PM by alessandro. 7 replies.

Sort Posts:

  • GridView not refreshing even after DataBind

    06-26-2009, 12:18 PM
    • Member
      10 point Member
    • bhern
    • Member since 06-15-2005, 4:26 PM
    • Posts 6

    I have a GridView with a SqlDataSource that displays general inventory information. The SqlDataSource is a stored proc that returns data successfully. Tied to the GridView is a DetailsView that displays detailed part information as parts are selected in the GridView. This is working very well. 

    When the DetailsView is displayed, I also provide several (server side) ImageButtons to perform transactions that update the underlying database -- adds, deletes, transfers, etc. When these transactions are completed, I want to refresh the GridView with the latest data.

    The code and updates are working correctly, but the GridView is not being refreshed. I can see the revised data simply by selecting a different row of the gridview after an update, and the data is refreshed.


    In the click_event of the transaction buttons, I am employing the following code to force a refresh of the GridView, but to no avail:


                    'Rebind the appropriate grid
                    grdOfficeProjectView.DataBind()
                    grdOfficeProjectView.SelectedIndex = -1
                    dvwProjectPart.DataBind()

                    'Rebind the appropriate grid

                    grdOfficeProjectView.DataBind()

                    grdOfficeProjectView.SelectedIndex = -1

                    dvwProjectPart.DataBind()


    where the grd and dvw vars represent the respective gridview and detailsview controls.


    What else should I be doing here? Is there a setting on the gridview that I'm missing? The underlying datasource does not have caching enabled, so it's not that. Appreciate your help or guidance.


  • Re: GridView not refreshing even after DataBind

    06-26-2009, 4:27 PM
    • Contributor
      2,141 point Contributor
    • rrmacman
    • Member since 06-10-2008, 7:04 PM
    • Cleveland, OH
    • Posts 453

    After you perform your transcations (delete, update, etc) you have to reset the datasource of the gridview with the new updated data, using just DataBind() for the grid wont cut it. So recall the method that binds the datasource to the gridview.

  • Re: GridView not refreshing even after DataBind

    06-26-2009, 5:19 PM
    • Member
      10 point Member
    • bhern
    • Member since 06-15-2005, 4:26 PM
    • Posts 6

     Thanks for the reply -- and apologies for what may be an ignorant follow-on, but what sequence of commands would perform that task (binding the datasource to the gridview)? I've set up the datasource and linked it to the gridview through the IDE, so I assume it's just the page load/initial rendering of the control that is performing that binding. In one variation as I was attempting to do this, I was setting the gridview's datasource to null/nothing, then reassigning it to the original datasource before performing the DataBind, but did not see any success with that, either.

    And in case you're wondering, I am trying to avoid doing a full page reload (Response.Redirect) to more efficiently/easily preserve state information.

    I'll keep researching the point you raised, however, as it certainly seems logical to require that sort of reset to see new data; if you can shortcut my journey, I'd be very appreciative.

  • Re: GridView not refreshing even after DataBind

    06-27-2009, 12:32 AM
    • Contributor
      6,788 point Contributor
    • alessandro
    • Member since 06-25-2002, 10:05 AM
    • Italy
    • Posts 1,103

    The detailsview has a ItemUpdated handler. This should fire right after your update operation has been successful (hopefully your image button performing the update has the commandname set to "Update". This is what triggers the whole operation allowing your ItemUpdating and ItemUpdated events to fire in sequence. Try calling databind on your gridview in ItemUpdated manually.

    Follow the sample code provided on msdn as below. Note that below they do not use any custom buttons and the default autogenerated buttons already have the commandname set to "Update" :

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.itemupdated.aspx



    Alessandro Zifiglio
    www.jiffycms.net - opensource HTML Editor for ASP.NET
    http://weblogs.asp.net/alessandro


    In the land of the blind, the man with one eye is king!:x
  • Re: GridView not refreshing even after DataBind

    06-27-2009, 1:54 AM
    • Member
      10 point Member
    • bhern
    • Member since 06-15-2005, 4:26 PM
    • Posts 6

     Alessandro --

    Thanks for your reply. I apologize, as I see in my original message I was imprecise in my description. The GridView and the linked DetailsView are both display-only in this setup -- I have a column of select buttons for the GridView to display the underlying detail in the DetailsView.

    *Outside* of those two controls, I have a set of text boxes and an accompanying set of imagebuttons that generate database updates/inserts/deletes that I want to see reflected in the GridView as they occur.

    Point being, none of the RowUpdated/ItemUpdated events will be fired in this setup.

  • Re: GridView not refreshing even after DataBind

    06-27-2009, 2:38 AM
    Answer
    • Contributor
      6,788 point Contributor
    • alessandro
    • Member since 06-25-2002, 10:05 AM
    • Italy
    • Posts 1,103

    i see. Unfortunately I'm unable to reproduce. The reason I asked you to go for the ItemUpdated handler was to ensure that the call to databind was made after the success of your update operation.

    At this point, since you stated you have your code running in a transaction, to make sure the transaction is committed etc. Try to ensure that your call to yourGridView.DataBind() is made after the change has been made permanently to your database.

    This is the only possible issue I am able guess at this point. try :

    using (SqlConnection conn = new SqlConnection(...))
    {
        //do what you gotta do inside this using block
        //associate a transaction to your sqlcommand object
        //executenonquery....
        //commit your transaction
    }
    myGridView.DataBind();

    Alessandro Zifiglio
    www.jiffycms.net - opensource HTML Editor for ASP.NET
    http://weblogs.asp.net/alessandro


    In the land of the blind, the man with one eye is king!:x
  • Re: GridView not refreshing even after DataBind

    07-06-2009, 10:15 AM
    • Member
      10 point Member
    • bhern
    • Member since 06-15-2005, 4:26 PM
    • Posts 6

    Alessandro -- thank you again. Still not able to get this to work. Let me include relevant code so that you or others might see where I'm going astray. I'm using a datacontext for the execution of the updates, which are accomplished via stored procedure. Within the stored proc I have defined the transaction commits and rollback logic.

    On the page:

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Myconnectionstring" SelectCommand="getQtyOnHand" SelectCommandType="StoredProcedure"></asp:SqlDataSource>

    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="Myconnectionstring" SelectCommand="getPartDetail" SelectCommandType="StoredProcedure"></asp:SqlDataSource>

    <asp:GridView ID="grdViewInventory" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="partNumber" DataSourceID="SqlDataSource1" ...> ... </asp:GridView>

    <asp:DetailsView ID="dvwPart" runat="server" AutoGenerateRows="False" DataKeyNames="partID" DataSourceID="SqlDataSource2" ...> ... </asp:DetailsView>

    The above portray a high level grid view of inventory by part, and a detailed view of part information when a part is selected in the grid view. This works fine. The objective is to refresh the gridview after the database changes as a result of other actions the user takes on this same page, which are handled in button handlers like the following:

    Dim selectedQty As Integer = CInt(txtQty.Text)

    Dim selectedPartID As Integer = dvwPart.SelectedValue

    Dim selectedOfficeID As Integer = CInt(grdOfficePartView.DataKeys(grdOfficePartView.SelectedIndex).Values("officeID"))

    Dim targetOfficeID As Integer = lstOffice.SelectedValue

    Dim returnCode As Integer = 0

    'Here's the actual transfer processing -- the transferInventory stored proc works correctly, updates the database, commits the transaction, etc.

    Dim dcInventory As New InventoryDataContext()

    returnCode = dcInventory.transferInventory(lblUserName.Text, selectedPartID, selectedQty, selectedOfficeID, targetOfficeID)

    if returnCode <> 0 then

    'Handle error, etc.

    else

    'Rebind the appropriate grid

    grdOfficePartView.DataBind()

    grdOfficePartView.SelectedIndex = -1

    dvwPart.DataBind()

    end if

     

    The rebind logic is run through, but has no impact on the visual display. When I change the view through any other means, the database changes are reflected in the refreshed grid, but not when I "force" the databind as above.

     

  • Re: GridView not refreshing even after DataBind

    07-06-2009, 3:33 PM
    • Contributor
      6,788 point Contributor
    • alessandro
    • Member since 06-25-2002, 10:05 AM
    • Italy
    • Posts 1,103

    It seems like you have data stagnation somewhere as the update is not reflected to the new  values in your underlying database, I SIMPLY am unable to see it. So here are a few questions that might help you further diagnose Tongue out

    1) Is the gridview of interest nested in an updatepanel? it maybe that you are not refreshing the apposite updatepanel where the gridview is nested. try to ensure that this is not the case

    2)in your pseudo code sample, the gridview you pasted is grdViewInventory, while in code  you are rebinding a different unrelated gridview, note the line below:

    grdOfficePartView.DataBind()

    is the above just a copy pasta error on your part? because if it is not then you seem to be calling databind on the wrong gridview.

    3) In your call to datacontext where you do the transferInventory try to make an explict call to submitchanges() as changes are put on que and then submit all at once and the change not being immediate might have some impact on you refreshing the gridview too soon (before the changes were really applied to the database), this is simply a guess as I do not use linq to sql. if that didn't fix it then perhaps you might want to start a new post in a linq to sql or EF forum if you suspect the problem to be in this area as you will get proper help there.

    Alessandro Zifiglio
    www.jiffycms.net - opensource HTML Editor for ASP.NET
    http://weblogs.asp.net/alessandro


    In the land of the blind, the man with one eye is king!:x
Page 1 of 1 (8 items)