Creating a Scrollable DataGrid…

來源:互聯網
上載者:User

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, 2003Download the code. Printer Friendly Version

br>

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.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.