<?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>Solomon &#187; DataGrid Paging</title>
	<atom:link href="http://www.ms.oyangudi.com/blog/tag/datagrid-paging/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ms.oyangudi.com/blog</link>
	<description>from Oyangudi...</description>
	<lastBuildDate>Tue, 18 Oct 2011 02:38:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>DataGrid Paging &#8211; C# Windows Forms</title>
		<link>http://www.ms.oyangudi.com/blog/csharp/datagrid-paging-c-windows-forms/</link>
		<comments>http://www.ms.oyangudi.com/blog/csharp/datagrid-paging-c-windows-forms/#comments</comments>
		<pubDate>Tue, 24 Feb 2009 04:34:51 +0000</pubDate>
		<dc:creator>Solomon</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[DataGrid Paging]]></category>

		<guid isPermaLink="false">http://www.ms.oyangudi.com/blog/?p=24</guid>
		<description><![CDATA[Download source files &#8211; 16.5 Kb Download demo project &#8211; 8.0 Kb Introduction This is an example of Windows Forms DataGrid paging using c#. I have used Microsoft SQL Server 2000 database. Using the code Create a table: In this example, the loadPage() method fetches only the required page data from the table using the [...]]]></description>
			<content:encoded><![CDATA[<div class="simplesocialbuttons">
<div class="simplesocialbutton ssb-button-googleplus"><!-- Google Plus One--><g:plusone size="medium" count="true" href="http://www.ms.oyangudi.com/blog/csharp/datagrid-paging-c-windows-forms/"></g:plusone></div>
<div class="simplesocialbutton ssb-button-fblike"><!-- Facebook like--><div id="fb-root"></div><fb:like href="http://www.ms.oyangudi.com/blog/csharp/datagrid-paging-c-windows-forms/" send="false" layout="button_count" width="100" show_faces="false" action="like" font=""></fb:like></div>
<div class="simplesocialbutton ssb-buttom-twitter"><!-- Twitter--><a name="twitter_share" data-count="horizontal" href="http://twitter.com/share" data-text="DataGrid Paging &#8211; C# Windows Forms" data-url="http://www.ms.oyangudi.com/blog/csharp/datagrid-paging-c-windows-forms/" class="twitter-share-button" rel="nofollow"></a></div>
</div>
<ul>
<li><a href="http://www.ms.oyangudi.com/blog/wp-content/uploads/DataGridPagingCWindowsForms_143E6/DataGridPaging_src_cs.zip">Download source files &#8211; 16.5 Kb</a> </li>
<li><a href="http://www.ms.oyangudi.com/blog/wp-content/uploads/DataGridPagingCWindowsForms_143E6/DataGridPaging_demo_cs.zip">Download demo project &#8211; 8.0 Kb</a> </li>
</ul>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="DataGridPaging" border="0" alt="DataGridPaging" src="http://www.ms.oyangudi.com/blog/wp-content/uploads/DataGridPagingCWindowsForms_143E6/DataGridPaging.gif" width="400" height="340" /> </p>
<h4><strong>Introduction</strong></h4>
<p>This is an example of Windows Forms DataGrid paging using c#. I have used Microsoft SQL Server 2000 database. </p>
<h4><strong>Using the code</strong></h4>
<p><strong>Create a table: </strong></p>
<div>
<pre class="brush: sql; title: ; notranslate">

create table tblEmp
    (
      E_ID int primary key ,
      E_Name varchar(60) ,
      E_Salary money ,
      E_DOJ datetime
    )
go
</pre>
</div>
<p>In this example, the loadPage() method fetches only the required page data from the table using the following select statement. </p>
<div>
<pre class="brush: csharp; title: ; notranslate">

		// Protected connection variable.
		protected SqlConnection mcnSample;

		// Page
		private int mintTotalRecords = 0;
		private int mintPageSize = 0;
		private int mintPageCount = 0;
		private int mintCurrentPage = 1;

		// Connection String
		protected const string CONNECTION_STRING = &quot;Server=localhost;UID=sa;PWD=;Database=Sample&quot;;
</pre>
</div>
<p>fillGrid() method: </p>
<div>
<pre class="brush: csharp; title: ; notranslate">

		private void fillGrid()
		{
			// For Page view.
			this.mintPageSize = int.Parse(this.tbPageSize.Text);
			this.mintTotalRecords = getCount();
			this.mintPageCount = this.mintTotalRecords / this.mintPageSize;

			// Adjust page count if the last page contains partial page.
			if (this.mintTotalRecords % this.mintPageSize &gt; 0)
				this.mintPageCount++;

			this.mintCurrentPage = 0;

			loadPage();
		}
</pre>
</div>
<p>getCount() method: This method gets the record count much faster than SELECT COUNT(*) statement. </p>
<div>
<pre class="brush: csharp; title: ; notranslate">

		private int getCount()
		{
			// This select statement is very fast compare to SELECT COUNT(*)
			string strSql = &quot;SELECT Rows FROM SYSINDEXES WHERE Id = OBJECT_ID('tblEmp') AND IndId &lt; 2&quot;;
			int intCount = 0;

			SqlCommand cmd = this.mcnSample.CreateCommand();
			cmd.CommandText = strSql;

			intCount = (int) cmd.ExecuteScalar();
			cmd.Dispose();

			return intCount;
		}
</pre>
</div>
<p>loadPage() method: </p>
<div>
<pre class="brush: csharp; title: ; notranslate">

		private void loadPage()
		{
			string strSql = &quot;&quot;;
			int intSkip = 0;

			intSkip = (this.mintCurrentPage * this.mintPageSize);

			// Select only the n records.
			strSql = &quot;SELECT TOP &quot; + this.mintPageSize +
				&quot; * FROM tblEmp WHERE E_Id NOT IN &quot; +
				&quot;(SELECT TOP &quot; + intSkip + &quot; E_Id FROM tblEmp)&quot;;

			SqlCommand cmd = this.mcnSample.CreateCommand();
			cmd.CommandText = strSql;

			SqlDataAdapter da = new SqlDataAdapter(cmd);

			DataSet ds = new DataSet();
			da.Fill(ds, &quot;tblEmp&quot;);

			// Populate Data Grid
			this.dgEmp.DataSource = ds.Tables[&quot;tblEmp&quot;].DefaultView;

			// Show Status
			this.lblStatus.Text = (this.mintCurrentPage + 1).ToString() + &quot; / &quot; + this.mintPageCount.ToString();

			cmd.Dispose();
			da.Dispose();
			ds.Dispose();
		}
</pre>
</div>
<p>For page navigation: </p>
<div>
<pre class="brush: csharp; title: ; notranslate">

		private void goFirst()
		{
			this.mintCurrentPage = 0;

			loadPage();
		}

		private void goPrevious()
		{
			if (this.mintCurrentPage == this.mintPageCount)
				this.mintCurrentPage = this.mintPageCount - 1;

			this.mintCurrentPage--;

			if (this.mintCurrentPage &lt; 1)
				this.mintCurrentPage = 0;

			loadPage();
		}

		private void goNext()
		{
			this.mintCurrentPage++;

			if (this.mintCurrentPage &gt; (this.mintPageCount - 1))
				this.mintCurrentPage = this.mintPageCount - 1;

			loadPage();
		}

		private void goLast()
		{
			this.mintCurrentPage = this.mintPageCount - 1;

			loadPage();
		}
</pre>
</div>
<h4>History</h4>
<p>Released on November 9th 2006. </p>
<div class="simplesocialbuttons">
<div class="simplesocialbutton ssb-button-googleplus"><!-- Google Plus One--><g:plusone size="medium" count="true" href="http://www.ms.oyangudi.com/blog/csharp/datagrid-paging-c-windows-forms/"></g:plusone></div>
<div class="simplesocialbutton ssb-button-fblike"><!-- Facebook like--><div id="fb-root"></div><fb:like href="http://www.ms.oyangudi.com/blog/csharp/datagrid-paging-c-windows-forms/" send="false" layout="button_count" width="100" show_faces="false" action="like" font=""></fb:like></div>
<div class="simplesocialbutton ssb-buttom-twitter"><!-- Twitter--><a name="twitter_share" data-count="horizontal" href="http://twitter.com/share" data-text="DataGrid Paging &#8211; C# Windows Forms" data-url="http://www.ms.oyangudi.com/blog/csharp/datagrid-paging-c-windows-forms/" class="twitter-share-button" rel="nofollow"></a></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.ms.oyangudi.com/blog/csharp/datagrid-paging-c-windows-forms/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

