Click here to Skip to main content
15,881,600 members
Articles / Programming Languages / C#
Tip/Trick

Docking a GridView and add a horizontal scrollbar

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
16 May 2012CPOL 59.3K   1.9K   12   3
Following article describes how to add a gridview and a horizontal scrollbar without depending on the screen resolution

Introduction

Following article shows you how to add a lengthy GridView and dock to available screen area. Docking is not depending on screen resolution.

Background

Following technologies (Jquery, CSS and C#) are used in following application.

Before using the JQuery.

Image 1

After using JQuery.

Image 2

Using the code

Add Jquery reference to the master page.

HTML
<script language="javascript" type="text/javascript" src="../Scripts/jquery-1.4.1.js"></script>

Create a style as follows. As you see width is specified as 200px initially. Later using the JQuery 200px is replaced with width of dvScreenWidth div.

HTML
<style type="text/css">
    .GridDock
    {
        overflow-x: auto;
        overflow-y: hidden;
        width: 200px;
        padding: 0 0 17px 0;
    }
</style>

Add a div to measure the screen width.

HTML
<div id="dvScreenWidth" visible="false"></div> 

Add another div around the GridView. This div display the scroll bar.

HTML
<div class="GridDock" id="dvGridWidth">
     <asp:GridView ID="GridView1" runat="server" Width="100%">
     </asp:GridView>
</div> 

Add the following JQuery scrip. Script will get the width of the dvScreenWidth div and replace the width of dvGridWidth div.

HTML
<script type="text/javascript">
    $(document).ready(function() {
       $('#dvGridWidth').width($('#dvScreenWidth').width());
    });
</script>

Populate the Gridview with dummy data.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
 
namespace DockingSample
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable gridDataDataTable = new DataTable();
            gridDataDataTable.Columns.Add("ID");
            gridDataDataTable.Columns.Add("Name");
            gridDataDataTable.Columns.Add("Address1");
            gridDataDataTable.Columns.Add("Address2");
            gridDataDataTable.Columns.Add("Address3");
            gridDataDataTable.Columns.Add("City");
            gridDataDataTable.Columns.Add("Zip");
            gridDataDataTable.Columns.Add("Province");
            gridDataDataTable.Columns.Add("Country");
            gridDataDataTable.Columns.Add("Sex");
            gridDataDataTable.Columns.Add("SNo");
            gridDataDataTable.Columns.Add("TelNo");
            gridDataDataTable.Columns.Add("FaxNo");
            gridDataDataTable.Columns.Add("MobileNo");
            gridDataDataTable.Columns.Add("VehicleNo");
            gridDataDataTable.Columns.Add("Color");
            gridDataDataTable.Columns.Add("Height");
            gridDataDataTable.Columns.Add("Weight");
            gridDataDataTable.Columns.Add("Company");
            gridDataDataTable.Columns.Add("CompanyAddress");
            gridDataDataTable.Columns.Add("CompanyTelNo");
            gridDataDataTable.Columns.Add("CompanyFaxNo");
 
            gridDataDataTable.Rows.Add("1", "Ryan", "City Stree1", "City Stree2", "City Stree1", "Barcelona", "232232", "N/A", "Spain", "Male", "S23343", "2223-232323-22", "2223-232323-22", "2223-232323-22", "2223-232323-22", "Red", "80cm", "75kg", "Lockheed Martin", "City Street3", "800-536145", "800-142587");
            gridDataDataTable.Rows.Add("2", "Ryan", "City Stree1", "City Stree2", "City Stree1", "Barcelona", "232232", "N/A", "Spain", "Male", "S23343", "2223-232323-22", "2223-232323-22", "2223-232323-22", "2223-232323-22", "Red", "80cm", "75kg", "Lockheed Martin", "City Street3", "800-536145", "800-142587");
            gridDataDataTable.Rows.Add("3", "Ryan", "City Stree1", "City Stree2", "City Stree1", "Barcelona", "232232", "N/A", "Spain", "Male", "S23343", "2223-232323-22", "2223-232323-22", "2223-232323-22", "2223-232323-22", "Red", "80cm", "75kg", "Lockheed Martin", "City Street3", "800-536145", "800-142587");
            
            GridView1.DataSource = gridDataDataTable;
            GridView1.DataBind();                      
 
        }
    }
}

Points of Interest

If we set the width as 100% for dvGridWidth div, width of the GridView will take the precedence. Then GridView width will bleed out of the screen and browser scroll will appear.

History

  • May 16, 2012: Article created.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Sri Lanka Sri Lanka
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
MohamedKamalPharm27-Jun-13 12:10
MohamedKamalPharm27-Jun-13 12:10 
GeneralMy vote of 5 Pin
serj197530-Oct-12 23:56
serj197530-Oct-12 23:56 
GeneralRe: My vote of 5 Pin
Member 87173437-Nov-12 21:37
Member 87173437-Nov-12 21:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.