/// <summary> /// 当用户点击分页编号按钮时引发此事件。 /// </summary> /// <param name="sender">分页按钮对象。</param> /// <param name="e">默认的时间数据。</param> private void PageNumberButton_Click(object sender, EventArgs e) { PageNumberButton btn = sender as PageNumberButton; //this.PageIndex = btn.PageNumber - 1; this.DoPageIndexChanging(btn.PageNumber - 1); }
/// <summary> /// 更新显示的分页按钮。 /// </summary> /// <param name="ABS">是否强制更新分页页码按钮。</param> private void RefreshPageNumberButtons(bool ABS) { int startPageIndex = this.PageIndex - this.PageGroupSize / 2; if (startPageIndex + this.PageGroupSize > this.PagesCount - 1) { startPageIndex = this.PagesCount - this.PageGroupSize; } if (startPageIndex < 0) { startPageIndex = 0; } this.FirstPageButton.Enabled = this.PreviousPageGroupButton.Enabled = this.PreviousPageButton.Enabled = (this.PageIndex > 0); this.LastPageButton.Enabled = this.NextPageGroupButton.Enabled = this.NextPageButton.Enabled = (this.PageIndex < this.PagesCount - 1); bool ReCreatePageNumberButtons = true; //指示是否需要重新设置分页编号按钮。 #region 判断是否需要重新设置分页编号按钮。 if (!ABS && this._ShowPageNumberButtons) { #region 找到当前显示的最大分页编号和最小分页编号。 int curSPI = 0; //最小分页编号。 int curMPI = 0; //最大分页编号。 bool first = true; foreach (var tmp in this.Items) { if (tmp is PageNumberButton || tmp is CurrentPageNumberButton) { int k; if (tmp is PageNumberButton) { k = ((PageNumberButton)tmp).PageNumber - 1; } else// if (tmp is CurrentPageNumberButton) { k = ((CurrentPageNumberButton)tmp).PageNumber - 1; } if (first) { first = false; curSPI = k; curMPI = k; } else { if (k < curSPI) { curSPI = k; } else if (k > curMPI) { curMPI = k; } } } } #endregion if (!first) //如果 first 发生反转则执行重新生成分页编号按钮的判断。 { int jj = startPageIndex + this.PageGroupSize - 1; if (jj >= this.PagesCount) { jj = this.PagesCount - 1; } if (curSPI == startPageIndex && curMPI == jj) { ReCreatePageNumberButtons = false; } } } #endregion #region 重新生成页码按钮。 if (!this._ShowPageNumberButtons || ReCreatePageNumberButtons) //当不显示分页编码按钮或需要重新创建分页编码按钮时,需要清除已存在的分页编码按钮。 { this.ClearPageNumberButton(); } if (this._ShowPageNumberButtons && ReCreatePageNumberButtons) { int insertIndex = this.Items.IndexOf(this.NextPageButton); //计算记录插入位置。 for (int i = startPageIndex; i < startPageIndex + this.PageGroupSize && i < this.PagesCount; i++) { if (i == this.PageIndex) //当前分页。 { CurrentPageNumberButton btn = new CurrentPageNumberButton(); btn.ForeColor = this.CurrentPageButtonForColor; btn.BackColor = this.CurrentPageButtonBackColor; btn.Font = this.CurrentPageButtonFont; btn.Text = (i + 1).ToString(); btn.ToolTipText = string.Format("第 {0} 页", i + 1); this.Items.Insert(insertIndex++, btn); } else { PageNumberButton btn = new PageNumberButton(i + 1); btn.ToolTipText = string.Format("第 {0} 页", i + 1); btn.Click += new EventHandler(this.PageNumberButton_Click); this.Items.Insert(insertIndex++, btn); } } } #endregion int crc = this.PageSize; //当前页记录总数 if (this.RecordsCount <= this.PageSize * (this.PageIndex + 1)) //如果尾页则需要统计记录实际数量。 { crc = this.RecordsCount - this.PageSize * this.PageIndex; } if (crc < 0) { crc = 0; } this._PageSizeLabel.Text = string.Format(this.PageSizeLabelFormatString, crc); this._RecordsCountLabel.Text = string.Format(this.RecordsCountLabelFormatString, this.RecordsCount); this._PageIndexLabel.Text = string.Format(this.PageIndexLabelFormatString, this.PageIndex + 1); this._PagesCountLabel.Text = string.Format(this.PagesCountLabelFormatString, this.PagesCount); #region 调整当前分页说明标签的位置,以满足按钮项目的布局合理性。 if (this.ShowPageIndexLabel && !this.ShowPageNumberButtons) { this.Items.Remove(this._PageIndexLabel); int insertIndex = this.Items.IndexOf(this._NextPageButton); if (insertIndex != -1) { this.Items.Insert(insertIndex, this._PageIndexLabel); } } else { this.Items.Remove(this._PageIndexLabel); int insertIndex = this.Items.IndexOf(this._PagesCountLabel); if (insertIndex != -1) { this.Items.Insert(insertIndex, this._PageIndexLabel); } } #endregion this.FixItemsPosition(); }