Creating a Scrollable DataGrid...

王朝c#·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航