Search code examples
c#asp.netgridviewupdatepanel

Gridview.Databind() not refresh the data


First of all, sorry if I make mistakes in english...

I'm making a web with c#, and I have some problems for refresh the data displayed in the GridView

I'm getting the data throw the SqlDataSource defined at the aspx view:

<asp:SqlDataSource ID="PRODUCTOS_CON_STOCK" runat="server" ConnectionString="<%$ ConnectionStrings:XXXX %>" ProviderName="<%$ ConnectionStrings:XXX.ProviderName %>" DataSourceMode="DataSet" SelectCommand=" select EAN, CODART.....  "> </asp:SqlDataSource>

When I click a button, I update some data in the database, and I want refresh de GridView with the new data, without reload the page. I'm making: gridView.DataBind();, but that doesn't refresh the data in the GridView.

(At the database is updated)

The GridView is inside of an UpdatePanel.

I was trying some things, like:

  • Reassing the DataSourceID and make the gridView.DataBind();

  • Assing the DataSourceID in null, make the gridView.DataBind();, and alter reassing the DataSourceID and make the gridView.DataBind();

  • I tried too:

    DataSourceSelectArguments argumentos = new DataSourceSelectArguments();
    
    PRODUCTOS_CON_STOCK.Select(argumentos);
    
    gridView.DataBind();
  • Set the UpdatePanel to updatemode="Always"

But any of all of that worked... Someone can help me?

Thanks.


Solution

  • Finally I resolved the issue.

    In my case, I was calling in the front, in AJAX, the C# method, that method is an WebMethod HttpPost

    The issue was that I was saving in a Session the GridView and the SqlDataSource, and although I was executing the Select statement again, I was obtaining the old's values.

    I was saving in a Session that information because the AJAX execute the method direct and the GridView and the SqlDataSource were null

    Finally I tried to make a refresh from the GridVeiw by a button, and the info is updated correctly, so, I hided the button, and simulate the click button in AJAX:

    $.ajax({
            type: 'POST',
            url: pageUrl,
            data: JSON.stringify({ gridMod: gridMod }),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function() {
                document.getElementById("<%= btnReload.ClientID %>").click();
            },
        });
    

    So, the click event call to LoadGrid();, and this one execute:

    private void LoadGrid()
        {
    
            DataSourceSelectArguments argumentos = new DataSourceSelectArguments();
    
            PRODUCTOS_CON_STOCK.Select(argumentos);
    
    
            eanList.DataBind();
    
    
        }
    

    In this way, I can refresh the data "automatically" without clicking in any button