Melanie
Melanie

Reputation: 584

Binding GridView in Code-Behind Not Working

I have a very annoying problem that I've been struggling with for a few hours now. I have multiple asp.net GridView controls on a page. One of these grids (grid B), is dependent on another grid (grid a) for its data to update correctly.

The problem I have is this:

When I do gridB.Databind() in a if(!IsPostback), then functionality of Grid B works, but the grid doesn't update with newly added records in Grid A.

When I do gridB.Databind() anywhere else that doesn't contain a Postback check, then the data in grid B updates correctly with the newly added records in Grid A, but then the functionality of Grid B no longer works.

Example:

            // Functionality for dropdownlist etc works correctly, but new data from gvA doesn't show in gvB
            if (grvSender.ID == "gvA")
            {
                if (!IsPostBack)
                    gvB.DataBind();
            }

            // Functionality for dropdownlist etc no longer works correctly, but new data from gvA shows correctly in gvB
            if (grvSender.ID == "gvA")
            {
                if (IsPostBack)
                    gvB.DataBind();
            }

I've called the databind method for gvB in every possible place known to man and the same problem persists...Fix problem A gives me problem B and fixing problem B gives me problem A.

Any ideas would be great. I can see this being something ridiculously silly but I've stared at the code almost all day now and I'm out of ideas.

Upvotes: 0

Views: 1098

Answers (1)

Amit
Amit

Reputation: 22086

You should use Page_PreRender event for your code and you can write your code as follow with else as well.

        if (grvSender.ID == "gvA")
        {
            if (!IsPostBack)
                gvB.DataBind();
        }

        else
        {
            if (IsPostBack)
                gvB.DataBind();
        }

Upvotes: 1

Related Questions