Home
Introduction
  Scrolling DataGrid
Author Vadivel Date created 19-February-2003
Webmail [email protected] Organization ScapeVelocity Net Solution Ltd.,
Check the author's other articles here (www.scapevelocity.com)

Scrollbars in DataGrid

In order to display a long list of items in a DataGrid with scrollbars rather than paging we need to make use div tag. Refer the sample source code provided below for further details.

The complete code listing follows:

<%@ Page language="c#" Codebehind="ScrollDatagrid.aspx.cs" AutoEventWireup="false" Inherits="testing.ScrollDatagrid" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
  <HEAD>
   <title>Scrolling Datagrid</title>
   <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
   <meta name="CODE_LANGUAGE" Content="C#">
   <meta name="vs_defaultClientScript" content="JavaScript"> 
   <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
  </HEAD>
  <body MS_POSITIONING="GridLayout">
   <form id="ScrollDatagrid" method="post" runat="server">
    <div id="div1" style="OVERFLOW: auto; HEIGHT: 250px">  
     <asp:DataGrid id="dtgrid" runat="server"></asp:DataGrid>
    </div>
   </form>
  </body>
</HTML>

Code behind file:

using System;
using System.Data;
using System.Data.SqlClient;
namespace testing
{
  public class ScrollDatagrid : System.Web.UI.Page
  {
   protected System.Web.UI.WebControls.DataGrid dtgrid;
   string ConnectionString;
   public ScrollDatagrid()
   {
    //Fetching the connection string from web.config file
    ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["constr"];
   }
   private void Page_Load(object sender, System.EventArgs e)
   {
    BindTable();
   }
   private void BindTable()
   {
    string strconn;
    SqlDataAdapter MySQLDA = new SqlDataAdapter();
    DataSet ds = new DataSet();			
    SqlConnection MySQLCns = new SqlConnection(ConnectionString);
    MySQLCns.Open();
    strconn = "select field1,field2 from tablename";
    MySQLDA = new SqlDataAdapter(strconn,MySQLCns);
    MySQLDA.Fill(ds);
    dtgrid.DataSource= ds;
    dtgrid.DataBind();
   }
   #region Web Form Designer generated code
   override protected void OnInit(EventArgs e)
   {
     //
     // CODEGEN: This call is required by the ASP.NET Web Form Designer.
     //
     InitializeComponent();
     base.OnInit(e);
   }
     /// <summary>
     /// Required method for Designer support - do not modify
     /// the contents of this method with the code editor.
     /// </summary>
     private void InitializeComponent()
     {    
       this.Load += new System.EventHandler(this.Page_Load);
     }
     #endregion
  }
}