October 2017 : Online course in ASP.NET MVC / Core. Conducted by Bipin Joshi. Read more...
Registration for October 2017 batch of ASP.NET MVC / Core online course has already started. Conducted by Bipin Joshi. Book your seat today ! Click here for more details.

<%@ Page %>

HOW TO: Display DataGrid in small screen area without using Paging

Introduction

Paging is perhaps one of the most frequently features of DataGrid web control. Paging provides following advantages:
  • Display small set of data from huge collection
  • Save browser window area by dividing entire data into pages
Paging requires you to post back the web form each time you want to change the page. Consider a case when you have say 100 records to be displayed. This is not really huge data for the kind of application you are developing. So, you do not really need paging. At the same time, however, you want to save browser window space because displaying 100 records is taking too much of screen space. How to overcome this? Read on...

Creating web form with DataGrid

We will first create a web form that contains a DataGrid bound with a database table. We will call this web form as WebForm1. The markup looks like this:
<%@ Page Language="vb"%>
<HTML>
<HEAD>
	<title>WebForm1</title>
</HEAD>
<body;>
	<form id="Form1" method="post" runat="server">
		<asp:DataGrid id="DataGrid1" runat="server">
		</asp:DataGrid>
	</form>
</body>
</HTML>
In the Page_Load event we will bind DataGrid as usual:
Dim connstr As String = 
"Integrated Security=SSPI;Initial Catalog=Northwind;
Data Source=SERVER\NetSDK"
Dim cnn As New SqlConnection(connstr)
Dim da As New SqlDataAdapter("select * from employees", cnn)
Dim ds As New DataSet()
da.Fill(ds, "employees")
DataGrid1.DataSource = ds
DataGrid1.DataBind()

Creating web form that hosts this DataGrid

Now, we will create the main form on which you intend to show the Data (say DataGridHost.aspx). We will use IFRAME HTML tag that will point to WebForm1 we developed previously. Following code show that:
<%@ Page Language="vb"%>
<html>
	<head>
		<title>DataGridHost</title>
	</head>
	<body;>
	<iframe src="webform1.aspx">
	</iframe>
	</body>
</html>
This form may have other web control just like any other web form. Once this form gets displayed the DataGrid from WebForm1 will be shown in the IFRAME. Note that IFRAME tage is specific to Internet Explorer.

Bipin Joshi is a software consultant, an author and a yoga mentor having 22+ years of experience in software development. He also conducts online courses in ASP.NET MVC / Core and Design Patterns. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced the Yoga way of life he also teaches Meditation and Mindfulness to interested individuals. To know more about him click here.

Get connected : Twitter  Facebook  Google+  LinkedIn

Posted On : 26 April 2002


Tags : ASP.NET Server Controls JavaScript Data Controls