Creating a Scrollable DataGrid...

类别:.NET开发 点击:0 评论:0 推荐:

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.

本文地址:http://com.8s8s.com/it/it43727.htm