• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

How do I directly change the contents of a GridView cell from within a PageIndexChanged sub?

When I update the GridViewfrom within the click method of a Button on the page, it works perfectly fine.  It instantly updates the cell on the GridView.

However, when I page forward and put the same command into the PageIndexChanged  method for the GridView, nothing happens.  No error and no update to the GridView.

I have created a label and put a command in the PageIndexChanged call to display what is in the particular cell and it does indeed display the correct contents from the GridView.

I've looked at the MSDN documentation, I've googled for similar problems and found nothing.  I'm running Visual Studio 2008 Pro on Vista SP2.  Website (ajax enabled) is coded in VB.

Would really appreciate some pointers as to what I'm missing here to be able to edit the GridView from inside the PageIndexChanged sub.

Please ask for any info if I have missed anything vital.
GridView1.Rows(0).Cells(3).Text = "TEST" ' when this is inside a Button's onclick code, it works perfectly fine.
Protected Sub GridView1_PageIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.PageIndexChanged
GridView1.Rows(0).Cells(3).Text = "TEST" ' doesn't do anything at all - no errors either.
End Sub
'This is the code for the GridView in the .aspx file, if it is of any help:
            <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
                AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="GUID" 
                DataSourceID="SqlDataSource1" style="font-family: Arial; font-size: xx-small">
                            <asp:CheckBox ID="Selector" runat="server" AutoPostBack="true" OnCheckedChanged="sCheckedChanged" />
                    <asp:BoundField DataField="GUID" HeaderText="GUID" ReadOnly="True" 
                        SortExpression="GUID" Visible="False" />
                    <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                    <asp:BoundField DataField="MsrcSeverity" HeaderText="MsrcSeverity" 
                        SortExpression="MsrcSeverity" />
                    <asp:BoundField DataField="SupportUrl" HeaderText="SupportUrl" 
                        SortExpression="SupportUrl" />

Open in new window

1 Solution
It's because after a page index change event occurs, the grid neeeds to rebind itself to the data source. You can use something like the c# code below on the data bound event to check if the rebind was due to a page event and set the cell text
public partial class _Default : System.Web.UI.Page
    bool pageHasChanged = false;
    protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        pageHasChanged = true;
    protected void GridView1_DataBound(object sender, EventArgs e)
        if (pageHasChanged)
            this.GridView1.Rows[0].Cells[1].Text = "TEST"; // doesn't do anything at all - no errors either.

Open in new window

riverbankAuthor Commented:
Perfect.  That was just the pointer I needed, thanks.  I had a play around and eventually got it to work around what you suggested.  Really simple when you know how.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now