﻿<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Programming By A Tool &#187; Paging</title>
	<atom:link href="http://byatool.com/tag/paging/feed/" rel="self" type="application/rss+xml" />
	<link>http://byatool.com</link>
	<description>This is my recorded stumbling through ASP.Net, Framework 3.5, C# 3.0, Ajax, Javascript, and Love.  Stay, learn, destroy.  It's your life.</description>
	<lastBuildDate>Thu, 03 May 2012 17:28:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Entity Framework: Reusable Paging Method</title>
		<link>http://byatool.com/c/entity-framework-reusable-paging-method/</link>
		<comments>http://byatool.com/c/entity-framework-reusable-paging-method/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 21:47:48 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Paging]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=724</guid>
		<description><![CDATA[OBSOLETE&#8230; sort of. Found an issue with this and am posting the issue today. (07/21/2009) A while back I decided to show some paging stuff and I forgot that recently, well ok in the last few months, I actually put together a sort of generic extension method to take care of most of the paging [...]]]></description>
			<content:encoded><![CDATA[<p><strong><a href="http://byatool.com/index.php/net-issues/entity-framework-linq-to-entities-only-supports-casting-entity-data-model-primitive-types/">OBSOLETE&#8230; sort of. Found an issue with this and am posting the issue today. (07/21/2009)</a></strong></p>
<p>A while back <a href="http://byatool.com/index.php/uncategorized/paging-and-the-entity-framework-skip-and-take-part-3">I decided to show some paging stuff</a> and I forgot that recently, well ok in the last few months, I actually put together a sort of generic extension method to take care of most of the paging situations I was running into. Reason being is I got tired of writing the same method over and over. I figured I&#8217;d share it in hopes that somehow, somewhere, it would keep someone&#8217;s dream alive. That and I figured I needed to make up for not posting for a while&#8230; which to some of you might have been a good thing.</p>
<p>Anywho, it&#8217;s not to difficult actually. Small note though, when reading the code you will see the <a href="http://byatool.com/index.php/uncategorized/paging-and-the-entity-framework-skip-and-take-part-1">TotalCountOfPages method</a> and the <a href="http://byatool.com/index.php/uncategorized/paging-and-the-entity-framework-skip-and-take-part-2">GetRealPage method</a>. If you can&#8217;t figure out what those are then I just don&#8217;t know what to do. (HINT: They are linked somewhere in this very post! Side note: This post is on track to match the normal self linking per post quota on <a href="http://www.codinghorror.com/blog/archives/001282.html">atypical Codding Horror post</a>)</p>
<pre><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #008080;">IList</span>&lt;K&gt; GetListForGrid&lt;T, K&gt;
(
  <span style="color: #0000ff;">this</span> <span style="color: #008080;">ObjectQuery</span>&lt;T&gt; query,  <span style="color: #008000;">//Extension method syntax... basically takes in any SomeEntities.SomeTableRepresentation</span>
  <span style="color: #008080;">Expression</span>&lt;<span style="color: #008080;">Func</span>&lt;T, <span style="color: #008080;">Boolean</span>&gt;&gt; somethingEqualsSomething,  <span style="color: #008000;">//Where clause</span>
  <span style="color: #008080;">Expression</span>&lt;<span style="color: #008080;">Func</span>&lt;T, K&gt;&gt; selectClause,
  <span style="color: #008080;">Expression</span>&lt;<span style="color: #008080;">Func</span>&lt;K, <span style="color: #008080;">IComparable</span>&gt;&gt; orderBy,  <span style="color: #008000;">//Turns out order by just <a href="http://byatool.com/index.php/general-coding/orderby-using-a-property-name/">needs anything that is IComparable</a></span>
  <span style="color: #008080;">Int32</span> pageNumber,
  <span style="color: #008080;">Int32</span> numberToShow,
  <span style="color: #0000ff;">out</span> <span style="color: #008080;">Int32</span> realPage,
  <span style="color: #0000ff;">out</span> <span style="color: #008080;">Int32</span> totalCountOfPages
)
{
   <span style="color: #008080;">IList</span>&lt;K&gt; returnValue;
   <span style="color: #008000;">//Get the total count of the items.  Need this to do proper paging.</span>
   <span style="color: #008080;">Int32</span> totalItemCount =
      query
      .Count
      (
         somethingEqualsSomething
      );

   totalCountOfPages = TotalCountOfPages(totalItemCount, numberToShow);
   realPage = GetRealPage(totalCountOfPages, pageNumber);

   returnValue = query
   .Where
   (
      somethingEqualsSomething
   )
   .Select
   (
      selectClause
   )
   .OrderBy(orderBy)
   .Skip(numberToShow * realPage)
   .Take(numberToShow)
   .ToList();

   <span style="color: #0000ff;">return</span> returnValue;
}</pre>
<p>As you can see there are a lot of expressions going on and that&#8217;s the way this works. It takes care of the annoying stuff that you would have to copy an paste while you supply it the stuff that changes like what you want to select and what the needed items have to match to be selected. Even has something for an order by. What the usage look like?</p>
<pre>  ToolEntities.Tools.GetListForGrid
  (
    something =&gt; something.SomethingId == passedInSomethingId,  <span style="color: #008000;">//Where clause
</span>    found =&gt; found,  <span style="color: #008000;">//Select: Simple here.  Usually I use classes to hold info... next post</span>
    found =&gt; found.Name,  <span style="color: #008000;">//Order by</span>
    pageNumber,
    amountToShow,
    <span style="color: #0000ff;">out</span> realPage,
    <span style="color: #0000ff;">out</span> totalCountOfPages
  );</pre>
<p>Really simple, eh?</p>
<p>Why totalCountOfPages and RealPage out? Well the whole idea is that this infomation is valuable to the picker control you will be constructing outside this method call. RealPage is nice for showing what page the pager is on and how to figure out what the previous and next pages will be. TotalCountOfPages is useful for the last page possible. Now the nice thing about the RealPage method is that it won&#8217;t give you a non existent page. It will only give you the best possible page if the pageNumber parameter is over the possible amount of pages.</p>
<p>Also do notice that this is an extention method meant to be used with the Entity Framework&#8217;s built in system of handling the Table representations say ToolEntities.Tools.(Method will show up here in intellisense). This makes it cleaner to me than having to pass in the actual current context. As in:</p>
<p>GetListForGrid(ToolEntities.Something, &#8230;) vs ToolEntites.Something.GetListForGrid(&#8230;)</p>
<p>In the end, either way is fine.</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/c/entity-framework-reusable-paging-method/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Paging and the Entity Framework, Skip, and Take Part 4</title>
		<link>http://byatool.com/utilities/paging-and-the-entity-framework-skip-and-take-part-4/</link>
		<comments>http://byatool.com/utilities/paging-and-the-entity-framework-skip-and-take-part-4/#comments</comments>
		<pubDate>Fri, 08 May 2009 20:08:11 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Utilities]]></category>
		<category><![CDATA[Paging]]></category>
		<category><![CDATA[WebControl]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=640</guid>
		<description><![CDATA[Get the total count of pages. &#124; Get the real page number. &#124; Using Skip and Take to Page &#124; The Actual Paging Controls Now onto the fourth part of this epic saga that George Washington once called, &#8220;The most astounding exercise in futility&#8221; Ok so let&#8217;s say you have a grid, items to fill [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://byatool.com/index.php/uncategorized/paging-and-the-entity-framework-skip-and-take-part-1">Get the total count of pages.</a> | <a href="http://byatool.com/index.php/uncategorized/paging-and-the-entity-framework-skip-and-take-part-2">Get the real page number.</a> | <a href="http://byatool.com/index.php/uncategorized/paging-and-the-entity-framework-skip-and-take-part-3">Using Skip and Take to Page</a> | <a href="http://byatool.com/index.php/uncategorized/paging-and-the-entity-framework-skip-and-take-part-4">The Actual Paging Controls</a></p>
<p>Now onto the fourth part of this epic saga that George Washington once called, &#8220;The most astounding exercise in futility&#8221;</p>
<p>Ok so let&#8217;s say you have a grid, items to fill it with, and four buttons. Each button has a direction, say one back, all the way back, one forward, all the way forward. No big deal. Now you want them to page correctly when used. After all, it&#8217;s always a bonus when things go right. First you need to set the CommandArguments on the buttons themselves to whatever page they are responsible for.</p>
<pre><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> SetAndBind(<span style="color: #008080;">Int32</span> pageNumber)
{
  <span style="color: #008080;">Int32</span> realPage;
  <span style="color: #008080;">Int32</span> totalCount;

  <span style="color: #008080;">IList&lt;ToolItem&gt;</span> inviteList =
    ToolClass
    .GetSomeTools(<span style="color: #800000;">"Sean"</span>, 20, pageNumber, <span style="color: #0000ff;">out</span> realPage, <span style="color: #0000ff;">out</span> totalCountOfPages);

  btnFullBack.CommandArgument = Convert.ToString(0);
  btnOneBack.CommandArgument = Convert.ToString(realPage - 1);
  btnOneForward.CommandArgument = Convert.ToString(realPage + 1);
  btnFullForward.CommandArgument = Convert.ToString(totalCountOfPages);

  BindGrid(inviteList);
}</pre>
<p>First off, GetSomeTools is just a method that is the same as I presented in the third installment of this epic saga that George Washington once called, &#8220;The most astounding exercise in futility&#8221;. If you&#8217;ve been reading up to this point, then you&#8217;ll know that. If not, you might have some reading to do.</p>
<p>So, here&#8217;s a simple method used to get the info we need (using the page number) and set the buttons. As seen before, realPage is the actual possible page (In case someone passed in page 21 when there are only 20 pages) and totalCountOfPages gives us how many possible pages there are. This makes setting the button CommandArguments cake. Now for the button event:</p>
<pre>  <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> btnPaging_Click(<span style="color: #008080;">Object</span> sender, <span style="color: #008080;">EventArgs</span> e)
  {
    SetAndBind(Convert.ToInt32(((<span style="color: #008080;">Button</span>)sender).CommandArgument));
  }</pre>
<p>And then you just set all the click events to that:</p>
<pre>  btnFullBack.Click += btnPaging_Click;
  btnOneBack.Click += btnPaging_Click;
  btnOneForward.Click += btnPaging_Click;
  btnFullForward.Click += btnPaging_Click;</pre>
<p>And boom, you&#8217;re pretty much set.</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/utilities/paging-and-the-entity-framework-skip-and-take-part-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Paging and the Entity Framework, Skip, and Take Part 2</title>
		<link>http://byatool.com/utilities/paging-and-the-entity-framework-skip-and-take-part-2/</link>
		<comments>http://byatool.com/utilities/paging-and-the-entity-framework-skip-and-take-part-2/#comments</comments>
		<pubDate>Thu, 07 May 2009 14:25:26 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Utilities]]></category>
		<category><![CDATA[Paging]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=626</guid>
		<description><![CDATA[Get the total count of pages. &#124; Get the real page number. &#124; Using Skip and Take to Page &#124; The Actual Paging Controls So in part one I posted the method to find the total count of pages you&#8217;ll need so that you don&#8217;t go too far while paging. Now it&#8217;s about trying to [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://byatool.com/index.php/uncategorized/paging-and-the-entity-framework-skip-and-take-part-1">Get the total count of pages.</a> | <a href="http://byatool.com/index.php/uncategorized/paging-and-the-entity-framework-skip-and-take-part-2">Get the real page number.</a> | <a href="http://byatool.com/index.php/uncategorized/paging-and-the-entity-framework-skip-and-take-part-3">Using Skip and Take to Page</a> | <a href="http://byatool.com/index.php/uncategorized/paging-and-the-entity-framework-skip-and-take-part-4">The Actual Paging Controls</a></p>
<p>So in part one I posted the method to find the total count of pages you&#8217;ll need so that you don&#8217;t go too far while paging. Now it&#8217;s about trying to get the best possible page number despite what&#8217;s passed in.</p>
<p>Here&#8217;s the situation, when you are paging up or down, you want to make sure you don&#8217;t go lower than 0 or higher than whatever the total count of pages is. Sometimes though, things go wrong. They go very wrong. It&#8217;s possible that your ui design will allow for any number to be passed in as the page number (Too many reasons this could happen to bother with). Say you have only 10 possible pages but the number 12 gets passed in. Well you either allow it to grab a non existant page 12 or you have a method to determine the best possible choice at this point (10). Turns out it&#8217;s idiot simple&#8230; what? Were you expecting anything different?</p>
<pre><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> Int32 GetRealPage(Int32 totalCountOfPages, Int32 pageNumber)
{
  <span style="color: #008000;">//pageNumber and totalCountOfPages have to be above 0 or problems
</span>  <span style="color: #0000ff;">if</span> (pageNumber &gt; 0 &amp;&amp; totalCountOfPages &gt; 0)
  {
    <span style="color: #008000;">//If page number is higher than the possible count of pages,</span>
    <span style="color: #008000;">//then you just need the total count to be the new page number...</span>
    <span style="color: #008000;">//but there's one more step</span>
    pageNumber = totalCountOfPages &lt; pageNumber ? totalCountOfPages : pageNumber;
    <span style="color: #008000;">//If by chance the pageNumber now is the same as totalCountOfPages</span>
    <span style="color: #008000;">//(Meaning it was larger than totalCountOfPages originally)</span>
    <span style="color: #008000;">//1 has to be subtracted to make sure it's inline with a 0 based system where</span>
    <span style="color: #008000;">//page 1 is actually 0.  This will make more sense when using skip and take in</span>
    <span style="color: #008000;">//the next post.</span>
    pageNumber = totalCountOfPages != pageNumber ? pageNumber : totalCountOfPages - 1;
  }
  <span style="color: #0000ff;">else</span>
  {
    pageNumber = 0;
  }

  <span style="color: #0000ff;">return</span> pageNumber;
}</pre>
<p>Ok so I&#8217;m going to claim this is amazing stuff, but it will come in handy with the next post.</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/utilities/paging-and-the-entity-framework-skip-and-take-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Paging and the Entity Framework, Skip, and Take Part 1</title>
		<link>http://byatool.com/utilities/paging-and-the-entity-framework-skip-and-take-part-1/</link>
		<comments>http://byatool.com/utilities/paging-and-the-entity-framework-skip-and-take-part-1/#comments</comments>
		<pubDate>Wed, 06 May 2009 13:41:44 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Utilities]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Paging]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=620</guid>
		<description><![CDATA[Get the total count of pages. &#124; Get the real page number. &#124; Using Skip and Take to Page &#124; The Actual Paging Controls So something I&#8217;ve been doing a lot of lately is making quite possibly the best thing ever: String cheese wrapped in turkey bacon. But when I&#8217;m not doing that, I am [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://byatool.com/index.php/uncategorized/paging-and-the-entity-framework-skip-and-take-part-1">Get the total count of pages.</a> | <a href="http://byatool.com/index.php/uncategorized/paging-and-the-entity-framework-skip-and-take-part-2">Get the real page number.</a> | <a href="http://byatool.com/index.php/uncategorized/paging-and-the-entity-framework-skip-and-take-part-3">Using Skip and Take to Page</a> | <a href="http://byatool.com/index.php/uncategorized/paging-and-the-entity-framework-skip-and-take-part-4">The Actual Paging Controls</a></p>
<p>So something I&#8217;ve been doing a lot of lately is making quite possibly the best thing ever: String cheese wrapped in turkey bacon. But when I&#8217;m not doing that, I am working with a lot of ListViews and the entity framework. Now I know there are &#8220;built in&#8221; paging controls but I just haven&#8217;t liked what I&#8217;ve seen. So I took it upon myself one day to develop a repeatable system for paging. Say you have users and a bunch of invites that are attached to the users. You want to show a list of invites for a particular user, but you want a ListView that doesn&#8217;t show every invite&#8230; ie you need a pager. Well just so happens I have a solution, but as always you should consult a doctor before use.</p>
<p>First off you need a method to get the total count of pages meaning what the ceiling is when you page upwards. After all, if you go over the possible count of pages, you&#8217;ll just get no items returned and look kind of dumb.</p>
<p>There are three situations you have to look out for: You have less items that the required items per page (Say 10 rows but you display 20 row per page), you have a perfect division (20 rows and 20 row per page or 40 rows and 20 rows per page, ect), you have an uneven division (21 rows and 20 rows  per page). First two are easy, third isn&#8217;t hard exactly but there are some catches.</p>
<pre><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #008080;">Int32</span> TotalCountOfPages(Int32 rowCount, Int32 neededPageSize)
{
<span style="color: #008080;">  Int32</span> returnValue;
<span style="color: #0000ff;">  if</span>(rowCount &gt; 0)
  {
    <span style="color: #0000ff;">if</span> (rowCount &lt;= neededPageSize)
    {
      returnValue = 1;
    }
    <span style="color: #0000ff;">else</span>
    {
      <span style="color: #0000ff;">if</span> ((rowCount % neededPageSize) == 0)
      {
        returnValue = rowCount / neededPageSize;
      }
      <span style="color: #0000ff;">else</span>
      {
        <span style="color: #008080;">Decimal</span> convertedPageSize =
         Convert.ToDecimal(neededPageSize);
        <span style="color: #008080;">Decimal</span> convertedRowCount =
         Convert.ToDecimal(rowCount);
        <span style="color: #008080;">Decimal</span> resultRounded =
          Math.Round(convertedRowCount / convertedPageSize);
        <span style="color: #008080;">Decimal</span> resultNonRounded =
          convertedRowCount / convertedPageSize;

        <span style="color: #0000ff;">if</span> (resultRounded &lt; resultNonRounded)
        {
           returnValue =
            Convert.ToInt32(resultRounded + 1);
        }
        <span style="color: #0000ff;">else</span>
        {
           returnValue =
            Convert.ToInt32(resultRounded);
        }
      }
    }
  }
<span style="color: #0000ff;">  else
</span>  {
    returnValue = 0;
  }
  <span style="color: #0000ff;">return</span> returnValue;
}</pre>
<p>Ok so first off, I assume this one is pretty obvious:</p>
<pre><span style="color: #0000ff;">  if</span>(rowCount &gt; 0)</pre>
<p>If there aren&#8217;t any rows, the there can&#8217;t be a page count.</p>
<p>Next take care the less rows than being shown per page:</p>
<pre>  <span style="color: #0000ff;">if</span> (rowCount &lt;= neededPageSize)
  {
    returnValue = 1;
  }</pre>
<p>Simple enough. Now for the second part, a perfect division between rows and rows to show:</p>
<pre> <span style="color: #0000ff;">if</span> ((rowCount % neededPageSize) == 0)
 {
    returnValue = rowCount / neededPageSize;
 }</pre>
<p>Basically, for those who don&#8217;t know mod or the % operator, that means there is no remainder. If there were, the result of rowCount % neededPageSize would not be 0 since Mod basically means &#8220;Give me what&#8217;s left over when I divide something by something else.&#8221;</p>
<p>Ok, this is where it gets a little messy as I have yet to find a good way to round a number up since, far as I know, there&#8217;s no way to do it with the .Net lib&#8217;ary. So, I had to come up with something clever&#8230; and when that failed I came up with this:</p>
<pre> <span style="color: #008080;">Decimal</span> convertedPageSize =
    Convert.ToDecimal(neededPageSize);
 <span style="color: #008080;">Decimal</span> convertedRowCount =
    Convert.ToDecimal(rowCount);
  <span style="color: #008080;">Decimal</span> resultRounded =
    Math.Round(convertedRowCount / convertedPageSize);
  <span style="color: #008080;">Decimal</span> resultNonRounded =
    convertedRowCount / convertedPageSize;

  <span style="color: #0000ff;">if</span> (resultRounded &lt; resultNonRounded)
  {
      returnValue =
          Convert.ToInt32(resultRounded + 1);
  }
  <span style="color: #0000ff;">else</span>
  {
     returnValue =
       Convert.ToInt32(resultRounded);
  }</pre>
<p>Ok so what&#8217;s going on here? First off, because trying to divide an integer by an integer gives me an integer, I had to covert some stuff to decimal. Why is that? When I divide them, I need to know if the number after the decimal is between 1 and 1.5 since Math.Round will round down in that case killing my ability to tell if I have enough pages.</p>
<p>Example: 19 rows and 10 per page will give me a 1.9 which Round will make 2. This is perfect since I need two pages to show the 19 rows. What if I have 14 rows and 10 per page? I get 1 from rounding. Uh oh. I really need two pages.</p>
<p>How do I get around this? Get the rounded version and the unrounded version. From there I know that if the unrounded version is greater than the rounded version I need an extra page.</p>
<p>So 14 rows / 10 per page = 1.4 unrounded and 1 rounded. 1.4 &gt; 1, so that 1 page isn&#8217;t going to do it. I need to add one more page. Now if it 19 rows/10 per page I&#8217;ll get 1.9 unrounded and 2 rounded. 2 pages is exactly what I need and I don&#8217;t have to add anything.</p>
<p>Next post I&#8217;ll show the next needed method: How to tell if the page number being sent in (From the pager/UI) is actually valid. If not, then grab the last possible amount of records.</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/utilities/paging-and-the-entity-framework-skip-and-take-part-1/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

