Place a DataGrid, or any other control or set of controls, within a scrollable region on your .NET web forms.
By: John Kilgo
Date: November 23, 2003
Download the code.
Printer Friendly Version
There are many times when it is inconvenient or unattractive to have a datagrid or other set of controls longer than the displayable page in the browser. I would prefer the user not to have to scroll the entire page if possible.
There is a way of accomplishing this that does not have anything at all to do with .NET. It actually has to do with using a style attribute within a
1<div> tag. While this is not a .NET technique, it may prove to be very useful to you in your .NET programming. The tag takes the form:
2
3<div style="OVERFLOW-Y:scroll; WIDTH:760px; HEIGHT:570px">
4
5The Width and Height styles allow you to determine the size of the scrollable region. The 760px and 570px shown above are just what I used for the example program. You can make them whatever you need them to be. The OVERFLOW-Y:scroll tells the browser to scroll the div vertically. All you have to do is place a table of objects, or a single object such as a datagrid within the DIV and it will be scrollable. You will be able to see this for yourself if you run the example program available at the bottom of this page, and/or download the sample code.
6
7The example .aspx file (ScrollingGrid.aspx), which demonstrates the technique is shown below.
@ Page Language="vb" AutoEventWireup="false" Codebehind="ScrollingGrid.aspx.vb" Inherits="DotNetJohn.ScrollingGrid"
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2
3<html>
4<head>
5<title>ScrollingGrid</title>
6<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR"/>
7<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE"/>
8<meta content="JavaScript" name="vs_defaultClientScript"/>
9<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema"/>
10</head>
11<body ms_positioning="GridLayout">
12<form id="Form1" method="post" runat="server">
13<table bgcolor="#EEEEEE" border="1" style="WIDTH: 780px; HEIGHT: 580px">
14<tr>
15<td align="center">Northwind Customers</td>
16<tr>
17<td>
18<div style="OVERFLOW-Y:scroll; WIDTH:760px; HEIGHT:570px">
19<table bgcolor="#FFFFFF">
20<tr>
21<td>
22<asp:datagrid backcolor="White" bordercolor="#999999" borderstyle="None" borderwidth="1px" cellpadding="3" gridlines="Vertical" id="dtgCusts" runat="server">
23<alternatingitemstyle backcolor="#DCDCDC"></alternatingitemstyle>
24<itemstyle backcolor="#EEEEEE" forecolor="Black"></itemstyle>
25<headerstyle backcolor="#000084" font-bold="True" forecolor="White"></headerstyle>
26</asp:datagrid>
27</td>
28</tr>
29</table>
30</div>
31</td>
32</tr>
33</tr></table>
34</form>
35</body>
36</html>
37---
38
39The .aspx.vb file, which just binds the datagrid to a datareader from the Northwind Customers table is shown below.
40
41Imports System.Data
42Imports System.Data.SqlClient
43Imports System.Configuration
44
45Public Class ScrollingGrid
46Inherits System.Web.UI.Page
47
48'-- Web Form Designer Generated Code Omitted --
49
50Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
51Dim strSql As String = "Select CustomerID, CompanyName, ContactName, Phone, Fax From Customers"
52Dim objConn As New SqlConnection(ConfigurationSettings.AppSettings("NorthwindConnection"))
53Dim objCmd As New SqlCommand(strSql, objConn)
54Try
55objConn.Open()
56dtgCusts.DataSource = objCmd.ExecuteReader()
57dtgCusts.DataBind()
58Catch ex As SqlException
59Response.Write(ex.ToString)
60Finally
61objCmd.Dispose()
62objConn.Dispose()
63End Try
64End Sub
65
66End Class
67---
68
69I hope you will find this technique useful in your .NET programming.
70
71You may run the program here.
72You may download the code here.</div></div>