Showing posts with label GridView. Show all posts
Showing posts with label GridView. Show all posts

Tuesday, August 4, 2009

Display Grid Page info ("Page # of Total Page Count")

The GridView control has the options to enable paging.  You can also specify different paging modes (NextPreviousFirstLast, Numeric, etc.) but it does not provide a way to display page info in the format of "Page 1 of 10" when you're in the mode of NextPriviousFirstLast mode.  So I decided to add my own.  Below the GridView control, I added a label control as follows:



I don't like to hard code strings into code if I can avoid it for internationlization reasons and for ease of editing at one place.  So I put all my messages in a resource file.   To display "Page # of #", I create a string resource named "PageNumberOfTotal" and its content is "Page {0} of {1}".  Then I handle the DataBound event of the GridView control as follows:

        protected void gdRecords_DataBound(object sender, EventArgs e)
        {
//If there are records to display, then display the message
            if (gdRecords.Rows.Count > 0)
            {
                lblNumberOfRecords.Visible = true;
                lblNumberOfRecords.Text = string.Format(
                   MessageResource.Properties.Resources.PageNumberOfTotal,
                    gdRecords.PageIndex + 1,
                    gdRecords.PageCount);
            } //If there isn't any records to display, the hide the label control
            else lblNumberOfRecords.Visible = false;
        }

About Cullen

My photo
Christian, Father, Software Developer/Architect who enjoys technology and using it to make people's lives easier!

Followers