datagrid分页问题(前后跳页)《控件版继承webcontrol》

之前是一个用户控件改写的,最近自己要用,就做了一个,效果还可以,以下代码参考:

using System;
using System.Data;
using System.Drawing;
using System.Text;
using System.Web;
using System.Web.UI;
using System.ComponentModel;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebControls
{

 [DefaultProperty("Paging"), DefaultEvent("PageIndexChanged"), ControlBuilderAttribute(typeof(DataGridPageBuilder)),
  ParseChildren(false), PersistChildren(false), Designer(typeof(DataGridPageDesigner)),
  ToolboxData("<{0}:DataGridPage runat=server></{0}:DataGridPage>")]
 public class DataGridPage : WebControl, IPostBackEventHandler
 {
  private string tmpID;
  private Unit tmpFirstImagebuttonCellWidth;
  private Unit tmpPreviousImagebuttonCellWidth;
  private Unit tmpNextImagebuttonCellWidth;
  private Unit tmpLastImagebuttonCellWidth;
  private Unit tmpPageTextLabelCellWidth;
  private Unit tmpCurrentPageTextCellWidth;
  private Unit tmpShowPageRolbtnCellWidth;
  private string tmpDataGridId;
  private ImageButtonEnable conFirstImagebutton;
  private ImageButtonEnable conPreviousImagebutton;
  private ImageButtonEnable conNextImagebutton;
  private ImageButtonEnable conLastImagebutton;
  private Label conPageTextLabel;
  private TextBox conCurPageTextBox;
  private ImageButtonEnable conShowPageImageButtonEnable;
  private DataGrid conDataGrid;
  public event EventHandler PageIndexChanged;
  public event EventHandler TextBoxEnterReturn;
  public event EventHandler ButtonClicked;
  

#region "property"
  [Category("Data"), DefaultValue(""), NotifyParentProperty(true), Description("datagrid")]
  public string TargetDataGrid
  {
   get
   {
    return tmpDataGridId;
   }
   set
   {
    tmpDataGridId = value;
   }
  }

  [NotifyParentProperty(true), PersistenceMode(PersistenceMode.InnerProperty), Category("ChildControl"), Description("最前页")]
  public ImageButtonEnable SubFirstImageButton
  {
   get
   {
    this.EnsureChildControls();
    return conFirstImagebutton;
   }
  }

  [NotifyParentProperty(true), PersistenceMode(PersistenceMode.InnerProperty), Category("ChildControl"), Description("前一页")]
  public ImageButtonEnable SubPreviousImageButton
  {
   get
   {
    this.EnsureChildControls();
    return conPreviousImagebutton;
   }
  }

  [NotifyParentProperty(true), PersistenceMode(PersistenceMode.InnerProperty), Category("ChildControl"), Description("下一页")]
  public TextBox SubCurPageTextBox
  {
   get
   {
    this.EnsureChildControls();
    return conCurPageTextBox;
   }
  }

  [NotifyParentProperty(true), PersistenceMode(PersistenceMode.InnerProperty), Category("ChildControl"), Description("页数")]
  public Label SubPageTextLabel
  {
   get
   {
    this.EnsureChildControls();
    return conPageTextLabel;
   }
  }

  [NotifyParentProperty(true), PersistenceMode(PersistenceMode.InnerProperty), Category("ChildControl"), Description("下一页")]
  public ImageButtonEnable SubNextImageButton
  {
   get
   {
    this.EnsureChildControls();
    return conNextImagebutton;
   }
  }

  [NotifyParentProperty(true), PersistenceMode(PersistenceMode.InnerProperty), Category("ChildControl"), Description("最后页")]
  public ImageButtonEnable SubLastImageButton
  {
   get
   {
    this.EnsureChildControls();
    return conLastImagebutton;
   }
  }

  [NotifyParentProperty(true), PersistenceMode(PersistenceMode.InnerProperty), Category("ChildControl"), Description("表示页")]
  public ImageButtonEnable SubShowPageImageButtonEnable
  {
   get
   {
    this.EnsureChildControls();
    return conShowPageImageButtonEnable;
   }
  }

  [Category("Appearance"), DefaultValue(typeof(Unit), "19px"), NotifyParentProperty(true), Description("最前页的宽度")]
  public Unit FirstImagebuttonCellWidth
  {
   get
   {
    return tmpFirstImagebuttonCellWidth;
   }
   set
   {
    tmpFirstImagebuttonCellWidth = value;
   }
  }

  [Category("Appearance"), DefaultValue(typeof(Unit), "12px"), NotifyParentProperty(true), Description("前一页的宽度")]
  public Unit PreviousImagebuttonCellWidth
  {
   get
   {
    return tmpPreviousImagebuttonCellWidth;
   }
   set
   {
    tmpPreviousImagebuttonCellWidth = value;
   }
  }

  [Category("Appearance"), DefaultValue(typeof(Unit), "31px"), NotifyParentProperty(true), Description("当前页")]
  public Unit CurPageTextBoxCellWidth
  {
   get
   {
    return tmpCurrentPageTextCellWidth;
   }
   set
   {
    tmpCurrentPageTextCellWidth = value;
   }
  }

  [Category("Appearance"), DefaultValue(typeof(Unit), "24px"), NotifyParentProperty(true), Description("页")]
  public Unit PageTextLabelCellWidth
  {
   get
   {
    return tmpPageTextLabelCellWidth;
   }
   set
   {
    tmpPageTextLabelCellWidth = value;
   }
  }

  [Category("Appearance"), DefaultValue(typeof(Unit), "12px"), NotifyParentProperty(true), Description("下一页的宽度")]
  public Unit NextImagebuttonCellWidth
  {
   get
   {
    return tmpNextImagebuttonCellWidth;
   }
   set
   {
    tmpNextImagebuttonCellWidth = value;
   }
  }

  [Category("Appearance"), DefaultValue(typeof(Unit), "19px"), NotifyParentProperty(true), Description("最后页的宽度")]
  public Unit LastImagebuttonCellWidth
  {
   get
   {
    return tmpLastImagebuttonCellWidth;
   }
   set
   {
    tmpLastImagebuttonCellWidth = value;
   }
  }

  [Category("Appearance"), DefaultValue(typeof(Unit), "0px"), NotifyParentProperty(true), Description("表示页的宽度")]
  public Unit ShowPageRolbtnCellWidth
  {
   get
   {
    return tmpShowPageRolbtnCellWidth;
   }
   set
   {
    tmpShowPageRolbtnCellWidth = value;
   }
  }

  [Category("Appearance"), DefaultValue(typeof(Unit), "0px"), NotifyParentProperty(true), Description("数据源")]
  public DataTable DataBindDataSource
  {
   get
   {
    return (DataTable)ViewState["DataBindDataSource"];
   }
   set
   {
    ViewState["DataBindDataSource"] = value;
   }
  }

  #region "PProperty"
  [NotifyParentProperty(true)]
  public override string ID
  {
   get
   {
    
    tmpID = (string)((ViewState["id"] == null) ? base.ID: ViewState["id"]);
    conFirstImagebutton.ID = tmpID + "_FirstImageButton";
    conPreviousImagebutton.ID = tmpID + "_PreviousImageButton";
    conPageTextLabel.ID = tmpID + "_PageTextLabel";
    conNextImagebutton.ID = tmpID + "_NextImageButton";
    conLastImagebutton.ID = tmpID + "_LastImageButton";
    conCurPageTextBox.ID = tmpID + "_CurPageTextBox";
    this.conShowPageImageButtonEnable.ID = tmpID + "_ShowPageImageButtonEnable";
    return tmpID;
   }
   set
   {
    ViewState["id"] = value;
   }
  }

  [NotifyParentProperty(true)]
  public override System.Drawing.Color BackColor
  {
   get
   {
    return base.BackColor;
   }
   set
   {
    base.BackColor = value;
    if (!(conFirstImagebutton == null))
    {
     conFirstImagebutton.BackColor = value;
    }
    if (!(conPreviousImagebutton == null))
    {
     conPreviousImagebutton.BackColor = value;
    }
    if (!(conPageTextLabel == null))
    {
     conPageTextLabel.BackColor = value;
    }
    if (!(conNextImagebutton == null))
    {
     conNextImagebutton.BackColor = value;
    }
    if (!(conLastImagebutton == null))
    {
     conLastImagebutton.BackColor = value;
    }
    if (!(conCurPageTextBox == null))
    {
     conCurPageTextBox.BackColor = value;
    }
    if (!(conShowPageImageButtonEnable == null))
    {
     conShowPageImageButtonEnable.BackColor = value;
    }
   }
  }

  [NotifyParentProperty(true)]
  public override System.Drawing.Color BorderColor
  {
   get
   {
    return base.BorderColor;
   }
   set
   {
    base.BorderColor = value;
   }
  }

  [NotifyParentProperty(true)]
  public override BorderStyle BorderStyle
  {
   get
   {
    return base.BorderStyle;
   }
   set
   {
    base.BorderStyle = value;
   }
  }

  [NotifyParentProperty(true)]
  public override Unit BorderWidth
  {
   get
   {
    return base.BorderWidth;
   }
   set
   {
    base.BorderWidth = value;
   }
  }

  [NotifyParentProperty(true)]
  public override System.Drawing.Color ForeColor
  {
   get
   {
    return base.ForeColor;
   }
   set
   {
    base.ForeColor = value;
    if (!(conFirstImagebutton == null))
    {
     conFirstImagebutton.ForeColor = value;
    }
    if (!(conPreviousImagebutton == null))
    {
     conPreviousImagebutton.ForeColor = value;
    }
    if (!(conPageTextLabel == null))
    {
     conPageTextLabel.ForeColor = value;
    }
    if (!(conNextImagebutton == null))
    {
     conNextImagebutton.ForeColor = value;
    }
    if (!(conLastImagebutton == null))
    {
     conLastImagebutton.ForeColor = value;
    }
    if (!(conCurPageTextBox == null))
    {
     conCurPageTextBox.ForeColor = value;
    }
    if (!(conShowPageImageButtonEnable == null))
    {
     conShowPageImageButtonEnable.ForeColor = value;
    }
   }
  }

  [NotifyParentProperty(true)]
  public override Unit Width
  {
   get
   {
    return base.Width;
   }
   set
   {
    base.Width = value;
   }
  }

  [NotifyParentProperty(true)]
  public override Unit Height
  {
   get
   {
    return base.Height;
   }
   set
   {
    base.Height = value;
   }
  }

  [NotifyParentProperty(true)]
  public override bool Enabled
  {
   get
   {
    return base.Enabled;
   }
   set
   {
    base.Enabled = value;
   }
  }

  [NotifyParentProperty(true)]
  public override bool Visible
  {
   get
   {
    return base.Visible;
   }
   set
   {
    base.Visible = value;
   }
  }

  [NotifyParentProperty(true)]
  public override string CssClass
  {
   get
   {
    return base.CssClass;
   }
   set
   {
    base.CssClass = value;
    if (!(conFirstImagebutton == null))
    {
     conFirstImagebutton.CssClass = value;
    }
    if (!(conPreviousImagebutton == null))
    {
     conPreviousImagebutton.CssClass = value;
    }
    if (!(conPageTextLabel == null))
    {
     conPageTextLabel.CssClass = value;
    }
    if (!(conNextImagebutton == null))
    {
     conNextImagebutton.CssClass = value;
    }
    if (!(conLastImagebutton == null))
    {
     conLastImagebutton.CssClass = value;
    }
    if (!(conCurPageTextBox == null))
    {
     conCurPageTextBox.CssClass = value;
    }
    if (!(conShowPageImageButtonEnable == null))
    {
     conShowPageImageButtonEnable.CssClass = value;
    }
   }
  }
#endregion
#endregion

  public DataGridPage()
  {
   this.FirstImagebuttonCellWidth = Unit.Pixel(19);
   this.PreviousImagebuttonCellWidth = Unit.Pixel(12);
   this.PageTextLabelCellWidth = Unit.Pixel(24);
   this.NextImagebuttonCellWidth = Unit.Pixel(12);
   this.LastImagebuttonCellWidth = Unit.Pixel(19);
   this.CurPageTextBoxCellWidth = Unit.Pixel(31);
   this.ShowPageRolbtnCellWidth = Unit.Pixel(0);
  }

  private bool IsDesignMode()
  {
   if ((HttpContext.Current == null))
   {
    return true;
   }
   else
   {
    return false;
   }
  }

  protected override void CreateChildControls()
  {
   if (conFirstImagebutton == null)
   {
    conFirstImagebutton = new ImageButtonEnable();
   }
   if (conPreviousImagebutton == null)
   {
    conPreviousImagebutton = new ImageButtonEnable();
   }
   if (conCurPageTextBox == null)
   {
    conCurPageTextBox = new TextBox();
    conCurPageTextBox.Width = Unit.Pixel(28);
    conCurPageTextBox.Text = "1";
   }
   if (conPageTextLabel == null)
   {
    conPageTextLabel = new Label();
    conPageTextLabel.Text = " / 1";
   }
   if (conNextImagebutton == null)
   {
    conNextImagebutton = new ImageButtonEnable();
   }
   if (conLastImagebutton == null)
   {
    conLastImagebutton = new ImageButtonEnable();
   }
   if (conShowPageImageButtonEnable == null)
   {
    conShowPageImageButtonEnable = new ImageButtonEnable();
   }

   this.SubFirstImageButton.Click += new System.Web.UI.ImageClickEventHandler(this.conFirstImagebutton_Click);
   this.SubNextImageButton.Click += new System.Web.UI.ImageClickEventHandler(this.conNextImagebutton_Click);
   this.SubPreviousImageButton.Click += new System.Web.UI.ImageClickEventHandler(this.conPreviousImagebutton_Click);
   this.SubLastImageButton.Click += new System.Web.UI.ImageClickEventHandler(this.conLastImagebutton_Click);
   this.SubShowPageImageButtonEnable.Click  += new System.Web.UI.ImageClickEventHandler(this.conShowPageRolloverImageButton_Click);

   SetChildLayout();
  }
  
  private void SetChildLayout()
  {
   Table htmlTable;
   TableRow htmlTableRow;
   TableRow nullTableRow;
   htmlTable = new Table();
   htmlTableRow = new TableRow();
   htmlTable.Rows.Add(htmlTableRow);
   htmlTable.BorderWidth = Unit.Pixel(0);
   htmlTable.CellPadding = 0;
   htmlTable.CellSpacing = 0;
   htmlTableRow.Cells.Add(new TableCell());
   htmlTableRow.Cells[0].Width = tmpFirstImagebuttonCellWidth;
   htmlTableRow.Cells[0].HorizontalAlign = HorizontalAlign.Left;
   htmlTableRow.Cells[0].VerticalAlign = VerticalAlign.Bottom;
   htmlTableRow.Cells[0].Controls.Add(conFirstImagebutton);
   htmlTableRow.Cells.Add(new TableCell());
   htmlTableRow.Cells[1].Width = this.tmpPreviousImagebuttonCellWidth;
   htmlTableRow.Cells[1].HorizontalAlign = HorizontalAlign.Left;
   htmlTableRow.Cells[1].VerticalAlign = VerticalAlign.Bottom;
   htmlTableRow.Cells[1].Controls.Add(conPreviousImagebutton);
   htmlTableRow.Cells.Add(new TableCell());
   htmlTableRow.Cells[2].RowSpan = 2;
   htmlTableRow.Cells[2].Width = this.tmpCurrentPageTextCellWidth;
   htmlTableRow.Cells[2].HorizontalAlign = HorizontalAlign.Left;
   htmlTableRow.Cells[2].VerticalAlign = VerticalAlign.Bottom;
   htmlTableRow.Cells[2].Controls.Add(this.conCurPageTextBox);
   htmlTableRow.Cells.Add(new TableCell());
   htmlTableRow.Cells[3].Width = this.tmpPageTextLabelCellWidth;
   htmlTableRow.Cells[3].HorizontalAlign = HorizontalAlign.Left;
   htmlTableRow.Cells[3].VerticalAlign = VerticalAlign.Bottom;
   htmlTableRow.Cells[3].Controls.Add(conPageTextLabel);
   htmlTableRow.Cells.Add(new TableCell());
   htmlTableRow.Cells[4].Width = this.tmpNextImagebuttonCellWidth;
   htmlTableRow.Cells[4].HorizontalAlign = HorizontalAlign.Left;
   htmlTableRow.Cells[4].VerticalAlign = VerticalAlign.Bottom;
   htmlTableRow.Cells[4].Controls.Add(conNextImagebutton);
   htmlTableRow.Cells.Add(new TableCell());
   htmlTableRow.Cells[5].Width = this.tmpLastImagebuttonCellWidth;
   htmlTableRow.Cells[5].HorizontalAlign = HorizontalAlign.Left;
   htmlTableRow.Cells[5].VerticalAlign = VerticalAlign.Bottom;
   htmlTableRow.Cells[5].Controls.Add(conLastImagebutton);
   htmlTableRow.Cells.Add(new TableCell());
   htmlTableRow.Cells[6].Width = this.tmpLastImagebuttonCellWidth;
   htmlTableRow.Cells[6].HorizontalAlign = HorizontalAlign.Left;
   htmlTableRow.Cells[6].VerticalAlign = VerticalAlign.Bottom;
   htmlTableRow.Cells[6].Controls.Add(this.conShowPageImageButtonEnable);
   nullTableRow = new TableRow();
   htmlTable.Rows.Add(nullTableRow);
   nullTableRow.Cells.Add(new TableCell());
   nullTableRow.Cells[0].Height = Unit.Pixel(2);
   nullTableRow.Cells.Add(new TableCell());
   nullTableRow.Cells[1].Height = Unit.Pixel(2);
   nullTableRow.Cells.Add(new TableCell());
   nullTableRow.Cells[2].Height = Unit.Pixel(2);
   nullTableRow.Cells.Add(new TableCell());
   nullTableRow.Cells[3].Height = Unit.Pixel(2);
   nullTableRow.Cells.Add(new TableCell());
   nullTableRow.Cells[4].Height = Unit.Pixel(2);
   nullTableRow.Cells.Add(new TableCell());
   nullTableRow.Cells[5].Height = Unit.Pixel(2);
   nullTableRow.Cells.Add(new TableCell());
   nullTableRow.Cells[6].Height = Unit.Pixel(2);
   base.Controls.Add(htmlTable);
  }
  protected override void OnLoad(EventArgs e)
  {
   Paging_Load(this,e);
   base.OnLoad (e);
  }

  protected override void Render(HtmlTextWriter writer)
  {
   this.EnsureChildControls();

   if (IsDesignMode())
   {
    base.Controls.Clear();
    SetChildLayout();
   }
  
   base.Render(writer);
  }

  protected override void AddParsedSubObject(object obj)
  {
   string[] idName;
   string tmp;

   if ((obj) is ImageButtonEnable)
   {
    idName = ((ImageButtonEnable)obj).ID.Split(((string)("_")).ToCharArray());
    tmp=idName[idName.Length -1].ToString();
    
    if (tmp.Equals("FirstImageButton"))
    {
     this.conFirstImagebutton = (ImageButtonEnable)obj;
    }
    if (tmp.Equals("PreviousImageButton"))
    {
     this.conPreviousImagebutton = (ImageButtonEnable)obj;
    }
    if (tmp.Equals("NextImageButton"))
    {
     this.conNextImagebutton = (ImageButtonEnable)obj;
    }
    if (tmp.Equals("LastImageButton"))
    {
     this.conLastImagebutton = (ImageButtonEnable)obj;
    }
   }
   if ((obj) is Label)
   {
    this.conPageTextLabel = (Label)obj;
   }
   if ((obj) is TextBox)
   {
    this.conCurPageTextBox = (TextBox)obj;
   }
   if ((obj) is ImageButtonEnable)
   {
    this.conShowPageImageButtonEnable = (ImageButtonEnable)obj;
   }

  }


  private void conShowPageRolloverImageButton_Click(object sender, System.Web.UI.ImageClickEventArgs e)
  {
   int pageNum;
   bool errFlag;
   errFlag = false;

   if (!(conCurPageTextBox.Text.Equals("")) && isNumber(conCurPageTextBox.Text))
   {
    pageNum = int.Parse(this.conCurPageTextBox.Text);

    if (pageNum < 1 | conDataGrid.PageCount < pageNum)
    {
     errFlag = true;
    }
   }
   else
   {
    pageNum=1;
    errFlag = true;
   }

   if (errFlag)
   {
    conCurPageTextBox.Text=System.Convert.ToString(conDataGrid.PageCount);
    conDataGrid.CurrentPageIndex = conDataGrid.PageCount - 1;
    ReDataBind(sender, e);
   }
   else
   {
     conDataGrid.CurrentPageIndex = pageNum - 1;
     ReDataBind(sender, e);
   }

   if (((Control)(sender)) is ImageButtonEnable)
   {
    if (ButtonClicked != null)
    {
     ButtonClicked(sender, e);
    }
   }
   else if (((Control)(sender)) is TextBox)
   {
    if (TextBoxEnterReturn != null)
    {
     TextBoxEnterReturn(sender, e);
    }
   }
  }

  bool isNumber(string strValue)
  {
   int intLoop;
   int intAsc;


   for (intLoop = 0; intLoop < strValue.Length; intLoop++)
   {
    intAsc = (int)strValue[intLoop];

    if (intAsc < 48 | 57 < intAsc)
    {
     return false;
    }
   }
   return true;
  }

  public void RaisePostBackEvent(string eventArgument)
  {
   System.Web.UI.ImageClickEventArgs e1 = new ImageClickEventArgs(0, 0);

   if (eventArgument.Equals(this.ID + "_EnterReturn"))
   {
    conShowPageRolloverImageButton_Click(conCurPageTextBox, e1);
   }
  }
  
  private void conFirstImagebutton_Click(object sender, System.Web.UI.ImageClickEventArgs e)
  {
   base.EnsureChildControls();
   conDataGrid.CurrentPageIndex = 0;
   ReDataBind(sender, e);
  }

  private void conPreviousImagebutton_Click(object sender, System.Web.UI.ImageClickEventArgs e)
  {
   base.EnsureChildControls();
   conDataGrid.CurrentPageIndex = Math.Max(conDataGrid.CurrentPageIndex - 1, 0);
   ReDataBind(sender, e);
  }

  private void conLastImagebutton_Click(object sender, System.Web.UI.ImageClickEventArgs e)
  {
   base.EnsureChildControls();
   conDataGrid.CurrentPageIndex = conDataGrid.PageCount - 1;
   ReDataBind(sender, e);
  }

  private void conNextImagebutton_Click(object sender, System.Web.UI.ImageClickEventArgs e)
  {
   base.EnsureChildControls();
   conDataGrid.CurrentPageIndex = Math.Min(conDataGrid.CurrentPageIndex + 1, conDataGrid.PageCount - 1);
   ReDataBind(sender, e);
  }

  private void ReDataBind(object sender, System.EventArgs e)
  {
   
   conDataGrid.DataSource =this.DataBindDataSource;
   conDataGrid.DataBind();
   ShowDataGridInfo();

   if (PageIndexChanged != null)
   {
    PageIndexChanged(sender, e);
   }
  }

  public void ShowDataGridInfo()
  {
   int tmpPageCount;
   int tmpCurrentPageIndex;
   
   SetPaingControl();
   tmpPageCount = conDataGrid.PageCount;
   tmpCurrentPageIndex = conDataGrid.CurrentPageIndex;

   this.conCurPageTextBox.Text = System.Convert.ToString(tmpCurrentPageIndex + 1);
         
   this.conShowPageImageButtonEnable.Enabled = this.conCurPageTextBox.Enabled;
   this.conPageTextLabel.Text = " / " + tmpPageCount;
   this.conFirstImagebutton.Enabled = (conDataGrid.CurrentPageIndex != 0);
   this.conPreviousImagebutton.Enabled = (conDataGrid.CurrentPageIndex != 0);
   this.conNextImagebutton.Enabled = (conDataGrid.CurrentPageIndex != conDataGrid.PageCount - 1);
   this.conLastImagebutton.Enabled = (conDataGrid.CurrentPageIndex != conDataGrid.PageCount - 1);
  }

  private void Paging_Load(object sender, System.EventArgs e)
  {
   SetPaingControl();
   if (!(Page.IsPostBack))
   {
    ShowDataGridInfo();
   }
  }

  private void SetPaingControl()
  {
   Control ctl;
   
   base.EnsureChildControls();
   if (tmpDataGridId == null)
   {
    throw new Exception("DataGrid没设置");
    return ;
   }
    
   ctl = Page.FindControl(tmpDataGridId);
   conDataGrid = ((DataGrid)(ctl));
   
   if (conDataGrid == null)
   {
    throw new Exception(tmpDataGridId + "不存在");
    return ;
   }
   
  }


 }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
WPF(Windows Presentation Foundation)是一种用于构建Windows客户端应用程序的框架,它提供了丰富的用户界面和数据绑定功能。DataGrid是WPF中的一个强大控件,可用于显示和编辑大量数据。 在WPF中,DataGrid默认不支持分页功能。但是可以通过自定义分页控件来实现这个功能。以下是一种实现WPF DataGrid分页控件的方法。 首先,我们可以创建一个自定义的UserControl来实现分页功能。这个UserControl可以包含一个DataGrid和一些用于翻页的按钮(如上一页、下一页、跳转到第一页和最后一页等)。 在UserControl中,我们可以定义一个依赖属性来绑定DataGrid的ItemsSource属性。这样,当我们在使用该分页控件时,只需要将数据源绑定到这个依赖属性即可。 接下来,在代码中,我们可以通过计算每页显示的数据数量,计算总页数,并根据当前页数和每页数据数量来筛选出相应的数据,然后将筛选后的数据绑定到DataGrid的ItemsSource属性上。 同时,我们可以为翻页按钮添加事件处理程序,以便在点击时切换到相应的页数,并更新DataGrid的显示内容。 最后,我们可以在XAML界面中使用这个分页控件,设置DataGrid的样式和布局,并绑定数据源到分页控件的依赖属性上。 总之,通过以上的步骤,我们可以实现一个自定义的WPF DataGrid分页控件,这个控件可以帮助我们在显示大量数据时进行分页,提升用户体验和程序性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值