Creating a Scrollable DataGrid...

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 <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:

<DIV style="OVERFLOW-Y:scroll; WIDTH:760px; HEIGHT:570px">

The 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.

The example .aspx file (ScrollingGrid.aspx), which demonstrates the technique is shown below.

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="ScrollingGrid.aspx.vb" Inherits="DotNetJohn.ScrollingGrid"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>ScrollingGrid</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name=vs_defaultClientScript content="JavaScript">
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<table border="1" bgcolor="#EEEEEE" style="WIDTH: 780px; HEIGHT: 580px">
  <tr>
    <td align="center">Northwind Customers</td>
  <tr>
    <td>
    <div style="OVERFLOW-Y:scroll; WIDTH:760px; HEIGHT:570px">
      <table bgcolor="#FFFFFF">
        <tr>
          <td>
            <asp:DataGrid ID="dtgCusts" Runat="server"
                          BorderColor="#999999" BorderStyle="None"
                          BorderWidth="1px" BackColor="White"
                          CellPadding="3" GridLines="Vertical">
              <AlternatingItemStyle BackColor="#DCDCDC"></AlternatingItemStyle>
              <ItemStyle ForeColor="Black" BackColor="#EEEEEE"></ItemStyle>
              <HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#000084"></HeaderStyle>
            </asp:DataGrid>
          </td>
        </tr>
      </table>
    </div>
    </td>
  </tr>
</table>
</form>
</body>
</HTML>

The .aspx.vb file, which just binds the datagrid to a datareader from the Northwind Customers table is shown below.

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Public Class ScrollingGrid
  Inherits System.Web.UI.Page

'-- Web Form Designer Generated Code Omitted --

  Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim strSql As String = "Select CustomerID, CompanyName, ContactName, Phone, Fax From Customers"
    Dim objConn As New SqlConnection(ConfigurationSettings.AppSettings("NorthwindConnection"))
    Dim objCmd As New SqlCommand(strSql, objConn)
    Try
      objConn.Open()
      dtgCusts.DataSource = objCmd.ExecuteReader()
      dtgCusts.DataBind()
    Catch ex As SqlException
      Response.Write(ex.ToString)
    Finally
      objCmd.Dispose()
      objConn.Dispose()
    End Try
  End Sub

End Class

I hope you will find this technique useful in your .NET programming.

You may run the program here.
You may download the code here.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值