locked
Refresh Gridview AfterInsert of Formview - c# code behind RRS feed

  • Question

  • User876561910 posted

    On a form I have
    MyGridview
    and
    MyFormView

    I am trying to get MyGridView to update after Insert of MyFormView.

    But it's not working.

    Any ideas?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.ComponentModel;
    
    
    
    
    public partial class MyForms_Form123 : System.Web.UI.Page
    {
        protected void MyFormView_ItemInserted(object sender, EventArgs e)
        {
            MyGridView.DataBind();
        }
         
    }
    
    
    


    Friday, September 16, 2016 3:38 PM

Answers

  • User876561910 posted

    It's OK.

    The problem was that I was using an ASP button and should have used an ASP LinkButton.
    I didn't see that in any documentation ??
    And, I'm not really sure why it would make a difference?
    Oh well.
     
    Something like this will work for most people (I think)

        protected void MyLinkButton_Click(object sender, EventArgs e)
        {
            FormViewName.InsertItem(true);
            GridViewName.DataBind();
        }

    Thanks for all the suggestions though

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 17, 2016 4:14 PM

All replies

  • User475983607 posted

    I am trying to get MyGridView to update after Insert of MyFormView.

    But it's not working.

    Did you remember to re-query the database after the insert and bind the new query result to the GridView?  

    Are you receiving an error?  

    Friday, September 16, 2016 5:35 PM
  • User876561910 posted

    Not sure what you mean.

    When I look at the DB the input data is there, I just need to auto-refresh the Gridview

    Friday, September 16, 2016 5:58 PM
  • User475983607 posted

    Not sure what you mean.

    When I look at the DB the input data is there, I just need to auto-refresh the Gridview

    After the insert, query the database to get the new data.  Set the Data Source of the GridView to the new result set and data bind.  

    Other than that, I 'm not sure what problem you are having.  The code snippet is not complete enough to provide assistance. 

    Friday, September 16, 2016 6:06 PM
  • User876561910 posted

    when I put the code as an OnClick of a button, it works
     
    It just doesn't work auto, which is what I'm trying to get

    Friday, September 16, 2016 6:27 PM
  • User475983607 posted

    janehollin

    when I put the code as an OnClick of a button, it works
     
    It just doesn't work auto, which is what I'm trying to get

    I have no idea what "work auto" means to you.  There is nothing automatic about refreshing a data grid.  The steps are update DB, query DB, assign the query results to the GridView data source, and finally data bind.  If you query the DB before the data is committed or don't query the data base at all then the DataGrid will not reflect the actual data.

    There is not enough code in the snippet to venture a guess where the bug is.

    Friday, September 16, 2016 6:40 PM
  • User-402182002 posted

    When you click the button, it will postback and refresh the page and the GridView. The auto function that you have mentioned, may not be refreshing the page and GridView.  

    Saturday, September 17, 2016 6:53 AM
  • User876561910 posted

    It's OK.

    The problem was that I was using an ASP button and should have used an ASP LinkButton.
    I didn't see that in any documentation ??
    And, I'm not really sure why it would make a difference?
    Oh well.
     
    Something like this will work for most people (I think)

        protected void MyLinkButton_Click(object sender, EventArgs e)
        {
            FormViewName.InsertItem(true);
            GridViewName.DataBind();
        }

    Thanks for all the suggestions though

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 17, 2016 4:14 PM