profile for Cullen Tsering at Stack Overflow, Q&A for professional and enthusiast programmers

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;
        }

0 comments:

Post a Comment

About Cullen

Cullen has been coding since 1991. He has been a Sr. Software Engineer/Architect for 11 years and counting. He enjoys coding for work as well as for fun. He loves learning from anyone and delights in sharing the little knowledge he possesses.

Followers