WPF paged Query

Source: Internet
Author: User

Paged query, very early heard, especially in the "university" of the project is more listening to more, has been thinking of their own research, to see what the mystery of the paging query. However, always because of their own doing other content, so has not been a good study. Now, when doing statistical query, because the query data volume is large, almost a second of a piece of data, or even a second 600 of data time query, so have to use paged query.

There are two types of paging queries that we often say. One is false paging, that is, a single time to the need to find all the data, and then displayed in the page is paginated. The benefit of this is to visually display the desired content to the user without scrolling through the scroll bar. In addition, the speed of paging is relatively fast.

Here I mainly want to summarize is another kind of paging-true pagination. The so-called true paging is what you see and what you get, and the data we see is the data we are currently querying. The advantage of this is that when we query a very large amount of data, such as when we query 10,000 of data, we can very quickly query the first display to the user of the part of the data, rather than go to the additional query at the moment does not need the content. Therefore, this greatly increases the speed of the query, improves the performance of the system, giving the user a very good user experience.

Here is an example code that I've done in my project for your reference:

<span style= "FONT-SIZE:24PX;"            > Public ucdatapage () {InitializeComponent ();            Pindex = 1; Loaded + = delegate {//home Btnfirst.mouseleftbuttonup + = new Mousebuttoneventhandl                               ER (btnfirst_click);                               Prev Btnprev.mouseleftbuttonup + = new Mousebuttoneventhandler (Btnprev_click);                               Next page Btnnext.mouseleftbuttonup + = new Mousebuttoneventhandler (Btnnext_click);                               Last Btnlast.mouseleftbuttonup + = new Mousebuttoneventhandler (Btnlast_click);            Btngo.click + = new Routedeventhandler (Btngo_click);        };        Private DataTable _dt = new DataTable ();        How many public static int pagenum = 10 is displayed per page;        is currently the first page of public static int pindex = 1;        Object public DataGrid grdlist; Max pages public static int Maxindex = 1;        Total number of records public static int allnum = 0;        public static int Count = 0;        <summary>///initialization data///</summary>//<param name= "GRD" ></param> <param name= "DS" ></param>///<param name= "Num" ></param> public void Showpages (D Atagrid GRD, DataTable ds, int Num) {if (ds = = null | | | ds.            Rows.Count = = 0) {return; } if (ds.            Rows.Count = = 0) {return;            } DataTable dt = ds; _DT = dt.            Clone ();            Grdlist = GRD;            Pagenum = Num;            Pindex = 1; foreach (DataRow r in dt. Rows) {_dt.            ImportRow (R);            } setmaxindex ();            Readdatatable ();                if (Maxindex > 1) {pagego.isreadonly = false;            Btngo.isenabled = true;  }      }///<summary>//Draw data///</summary> private void readdatatable () {                    Displaypaginginfo ();        }///<summary>//Draw data per page////</summary> private void Displaypaginginfo ()                {if (Pindex = = 1) {btnprev.isenabled = false;            btnfirst.isenabled = false;                } else {btnprev.isenabled = true;            Btnfirst.isenabled = true;                } if (Pindex = = maxindex) {btnnext.isenabled = false;            btnlast.isenabled = false;                } else {btnnext.isenabled = true;            Btnlast.isenabled = true; } Tbkrecords.text = string.            Format ("{0} per page/total {1} bar", Pagenum, Allnum); int first = (pIndex-4) > 0?            (pIndex-4): 1; int last = (First + 9) > Maxindex?            Maxindex: (first + 9); Grid.            Children.clear ();                for (int i = First, I <= last; i++) {columndefinition cdf = new ColumnDefinition (); Grid.                Columndefinitions.add (CDF);                TextBlock tbl = new TextBlock (); Tbl.                Text = i.ToString (); Tbl.                style = FindResource ("PageTextBlock3") as Style; Tbl.                MouseLeftButtonUp + = new Mousebuttoneventhandler (tbl_mouseleftbuttonup); Tbl.                MouseLeftButtonDown + = new Mousebuttoneventhandler (Tbl_mouseleftbuttondown); if (i = = Pindex) {tbl.                IsEnabled = false; } grid.setcolumn (TBL, Grid.                COLUMNDEFINITIONS.COUNT-1);                Grid.setrow (TBL, 0); Grid.            Children.add (TBL); }}///<summary>//home///</summary>/<param name= "Sender" ><  /param>      <param name= "E" ></param> private void Btnfirst_click (object sender, System.EventArgs e)            {pindex = 1;            Raisepagechanged ();        Readdatatable (); }///<summary>//home//</summary>/<param name= "Sender" ></param&gt        ; <param name= "E" ></param> private void Btnfirst_mouseleftbuttondown (object sender, Mousebuttoneventa        RGS e) {e.handled = true; }///<summary>//Previous page//</summary>//<param name= "Sender" &GT;&LT;/PARAM&G        T            <param name= "E" ></param> private void Btnprev_click (object sender, System.EventArgs e) {            if (pindex <= 1) {return;            } pindex--;            Raisepagechanged ();        Readdatatable (); }//<summary>//Previous page//&LT;/SUmmary>//<param name= "sender" ></param>///<param name= "E" ></param> priv            ate void Btnprev_mouseleftbuttondown (object sender, MouseButtonEventArgs e) {e.handled = true;        Raisepagechanged (); }///<summary>//Next page///</summary>//<param name= "Sender" &GT;&LT;/PARAM&G        T            <param name= "E" ></param> private void Btnnext_click (object sender, System.EventArgs e) {            if (Pindex >= maxindex) {return;            } pindex++;            Raisepagechanged ();                     Readdatatable (); }///<summary>//Next page///</summary>//<param name= "Sender" &GT;&LT;/PARAM&G        T <param name= "E" ></param> private void Btnnext_mouseleftbuttondown (object sender, Mousebuttoneventar GS e) {E.handleD = true; }///<summary>//Last//</summary>/<param name= "Sender" ></param&gt        ;            <param name= "E" ></param> private void Btnlast_click (object sender, System.EventArgs e) {            Pindex = Maxindex;            Raisepagechanged ();        Readdatatable (); }///<summary>//Last//</summary>/<param name= "Sender" ></param&gt        ; <param name= "E" ></param> private void Btnlast_mouseleftbuttondown (object sender, Mousebuttoneventar        GS e) {e.handled = true;            }///<summary>//Set maximum page///</summary> private void Setmaxindex () {            How many pages int pages = Count/pagenum;                    if (count! = (pages * pagenum)) {if (Count < (pages * Pagenum)) {          pages--;      } else {pages++;                } Maxindex = Pages; Allnum = _dt.            Rows.Count; }}///<summary>///skip to how many pages///</summary>//<param name= "Sender" > </param>//<param name= "E" ></param> private void Btngo_click (object sender, Routedeventa RGS e) {if (Isnumber (Pagego.text)) {int pagenum = Int.                Parse (Pagego.text);                    if (Pagenum > 0 && pagenum <= maxindex) {pindex = Pagenum;                    Raisepagechanged ();                Readdatatable ();                    } else if (Pagenum > Maxindex) {pindex = Maxindex;                    Raisepagechanged ();                Readdatatable ();        }} pagego.text = "";   }     <summary>///pagination number of Click Trigger events///</summary>//<param name= "Sender" ></par am>//<param name= "E" ></param> private void Tbl_mouseleftbuttonup (object sender, Mousebutto            Neventargs e) {TextBlock tbl = sender as TextBlock;            if (TBL = = null) {return; } int index = Int. Parse (TBL.            Text.tostring ());            Pindex = index;            if (Index > Maxindex) {pindex = Maxindex;            } if (Index < 1) {pindex = 1;            } raisepagechanged ();        Readdatatable ();        } void Tbl_mouseleftbuttondown (object sender, MouseButtonEventArgs e) {e.handled = true;        } private static Regex regnumber = new Regex ("^[0-9]+$"); <summary>//Whether the figure is digital///</summary>//<paramName= "valstring" ></param>///<returns></returns> public static bool Isnumber (string VA            lstring) {Match m = Regnumber.match (valstring);        return m.success;        } #region field, property, delegate public delegate void Pagechangedeventhandler (object sender, Pagechangedeventargs args);        Private Pagechangedeventargs Pagechangedeventargs; #endregion///<summary>///Trigger Page Change events///</summary> private void raisepagechanged () {if (Pagechangedeventargs = = null) {Pagechangedeventargs = new Pagechange            Deventargs (Pagechangedevent, Pagenum, Pindex);                } else {pagechangedeventargs.pagesize = Pagenum;            Pagechangedeventargs.pageindex = Pindex;                   } raiseevent (Pagechangedeventargs); } public static readonly RoutedEvent pagechangedevent = EventManager.registerroutedevent ("pagechanged", Routingstrategy.bubble, typeof (Pagechangedeventhandler), typeof (        Ucdatapage));            public class Pagechangedeventargs:routedeventargs {public int PageSize {get; set;}            public int PageIndex {get; set;}            Public Pagechangedeventargs (RoutedEvent routeevent, int pageSize, int pageIndex): Base (routeevent)                {PageSize = PageSize;            PageIndex = PageIndex; }} public Event Pagechangedeventhandler pagechanged {add {add            Handler (pagechangedevent, value);            } remove {RemoveHandler (pagechangedevent, value); }}}}</span>

The above is the main computational logic, where true paging and false paging is the biggest difference is to get the current page index, per page capacity and other content as query criteria to implement the query function. In addition, a difference is the need to delegate to the function of paging into the project, the last of several methods is the implementation of the delegation.
Next is how to join the project, the first is to add the paging function as a user control to the project, the foreground code is as follows:

<span style= "FONT-SIZE:24PX;" ><pre name= "code" class= "HTML" > <my:ucdatapage grid.row= "1" horizontalalignment= "left" margin= "10,0,0,0" X:name= "Ucdatapage" pagechanged= "datapager_pagechanged"  verticalalignment= "Bottom" width= "1114"/></ Span>

The delegate method in the background:

<span style= "FONT-SIZE:24PX;" ><pre name= "code" class= "CSharp" >       </span>
<span style= "FONT-SIZE:24PX;" >private void Datapager_pagechanged (object sender, Ucdatapage.pagechangedeventargs args) {#region query                Data try {System.Data.DataTable dt = new System.Data.DataTable ();                DT = Query (strwhere, PageSize, Ucdatapage.pindex);                int pindex = Ucdatapage.pindex; Dt.          Defaultview.sort = "S_monitoringtime DESC"; The results of the query are displayed in reverse order with the IF (dt. Rows.Count = = 0) {MessageBox.Show ("There is no qualifying information for the time period you queried, please re-select the query time!")                    "," prompt "); dataGrid1.ItemsSource = dt.                    DefaultView;                    Btnquery.isenabled = true;                Return } else {//wind speed scale set for (int i = 0; i < dt. Rows.Count; i++) {DataRow row = dt.                        Rows[i]; if (row["I_alarmwindspeed"]! = NULL | |row["I_alarmwindspeed"]. ToString () = "") {row["i_alarmwindspeed"] = convert.todouble (row["I_al Armwindspeed "]).                        ToString ("F1");                            } if (Convert.isdbnull (row["i_windspeed1"]) = = False) { row["i_windspeed1"] = convert.todouble (row["i_windspeed1"]).                        ToString ("F1");                            } if (Convert.isdbnull (row["I_windspeed2"]) = = False) { row["I_windspeed2"] = convert.todouble (row["I_windspeed2"]).                        ToString ("F1"); }} datagrid1.itemssource = dt.                    DefaultView;                    Ucdatapage.showpages (dataGrid1, DT, PageSize);                    Btnquery.isenabled = true;                Ucdatapage.pindex = Pindex;            } dt = dt;    } catch (Exception ex) {            MessageBox.Show ("There was a problem querying the wind speed information, the error message is:" + ex.)                Message); Loginfo.writelog (LogInfo.LogLevel.Error, ex. StackTrace + "<br>" + ex. Message + "<br>" + "the position at which the action was raised is:" + this.                GetType () + "Btnquery_click () Method!");            Return            } finally {btnquery.isenabled = true; } #endregion}</span>

Summary , the above is true pagination of the specific implementation method, the whole process is relatively easy, I think the more complex is the package of the delegate, the implementation of the delegation, that is, the realization of the true page ...
<span style= "font-family:arial, Helvetica, sans-serif;font-size:24px;" ><span style= "White-space:normal;" ></span></span>

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

WPF paged Query

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.