Click here to Skip to main content
15,861,125 members
Articles / Web Development / ASP.NET
Tip/Trick

Resizable ASP.NET Gridview Columns using Jquery

Rate me:
Please Sign up or sign in to vote.
4.81/5 (14 votes)
14 Apr 2012CPOL 92.7K   4.1K   23   27
This tip explains how to use jQuery's plug-in colResizable to implement client-side column resizing in an ASP.NET GridView.

Introduction

This tip explains how to use jQuery's plug-in colResizable to implement client-side column resizing in an ASP.NET GridView.

Using the Code

In addition to colResizable, I also use Jquery cookie plugin to store columns width in a cookie, so that ASP.NET postback won't reset column width. Now, download and include the jQuery, cookie and colResizable JavaScript files as follows:

JavaScript
<script type="text/javascript" src="scripts/jquery-latest.js"/>
<script src="/Scripts/colResizable-1.3.min.js" type="text/javascript"/>
<script src="/Scripts/jquery.cookie.js" type="text/javascript"/>  

Initialize the table width from cookie and colResizable plugin when the document is ready, using the code below:

JavaScript
$(document).ready(function() 
{ 
    if ($.cookie('colWidth') != null) {
        var columns = $.cookie('colWidth').split(',');
        var i = 0;
        $('.GridViewStyle th').each(function () {
            $(this).width(columns[i++]);
        });
    }
 
    $(".GridViewStyle").colResizable({
        liveDrag: true,
        gripInnerHtml: "<div class='grip'></div>",
        draggingClass: "dragging",
        onResize: onSampleResized
    }); }); 

A callback function onSampleResized will be fired when the user has ended dragging a column anchor altering the previous table layout. It will read columns width and store them in a cookie.

JavaScript
var onSampleResized = function (e) {
    var columns = $(e.currentTarget).find("th");
    var msg = "";
    columns.each(function () { msg += $(this).width() + ","; })
    $.cookie("colWidth", msg);
};  

Here is an ASP.NET Gridview.

ASP.NET
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" <br />DataKeyNames="Id" GridLines="None" CssClass="GridViewStyle">
<Columns>
......
...... 
<Columns>
</asp:GridView> 

The final output can be seen here:

Summary

This tip showed how to Resize ASP.NET gridview using Jquery.

License

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


Written By
Software Developer (Senior) http://www.Fairnet.com
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionhow to resize particular columns only..i don't want to resize all columns.. Pin
bogala.net28-Dec-14 16:08
bogala.net28-Dec-14 16:08 
Questionhow to do column resizing when paging Pin
amnk.info23-Nov-14 16:55
amnk.info23-Nov-14 16:55 
QuestionResizing Columns Pin
ShaHam1120-Nov-13 0:07
ShaHam1120-Nov-13 0:07 
Questionabout gridview resizing Pin
kamleshnada18-Feb-13 19:25
kamleshnada18-Feb-13 19:25 
QuestionHow to reset the gridview columns to its default size on double clicking to the gridview lines? Pin
Member 933240112-Feb-13 19:11
Member 933240112-Feb-13 19:11 
QuestionDoesn't work after asynchronous postback when the GridView is in an UpdatePanel Pin
yashugg6-Feb-13 18:03
yashugg6-Feb-13 18:03 
AnswerRe: Doesn't work after asynchronous postback when the GridView is in an UpdatePanel Pin
GNDNeth9-Jan-14 15:48
GNDNeth9-Jan-14 15:48 
Questionadjusting gridview width manually by the user Pin
aassaahh6-Oct-12 7:54
aassaahh6-Oct-12 7:54 
GeneralMy vote of 3 Pin
Sanjay K. Gupta17-Sep-12 3:03
professionalSanjay K. Gupta17-Sep-12 3:03 
QuestionDoesn't work after asynchronous postback when the GridView is in an UpdatePanel Pin
Steverey422-Aug-12 20:27
Steverey422-Aug-12 20:27 
AnswerRe: Doesn't work after asynchronous postback when the GridView is in an UpdatePanel Pin
Steverey427-Aug-12 12:41
Steverey427-Aug-12 12:41 
GeneralRe: Doesn't work after asynchronous postback when the GridView is in an UpdatePanel Pin
yashugg6-Feb-13 18:02
yashugg6-Feb-13 18:02 
GeneralRe: Doesn't work after asynchronous postback when the GridView is in an UpdatePanel Pin
j chakradhar rao25-Mar-13 4:08
j chakradhar rao25-Mar-13 4:08 
GeneralRe: Doesn't work after asynchronous postback when the GridView is in an UpdatePanel Pin
mac_sak26-Nov-13 20:18
mac_sak26-Nov-13 20:18 
GeneralRe: Doesn't work after asynchronous postback when the GridView is in an UpdatePanel Pin
zainulaabdin8-Mar-14 17:07
zainulaabdin8-Mar-14 17:07 
QuestionThis script wont works on FF 13.0.0.1 Pin
Member 806778926-Jun-12 23:36
Member 806778926-Jun-12 23:36 
AnswerRe: This script wont works on FF 13.0.0.1 Pin
Farooq Kaiser27-Jun-12 7:28
professionalFarooq Kaiser27-Jun-12 7:28 
QuestionGreat idea! my vote of 5! but little problems on resizing TemplateFields Pin
stefan999913-Jun-12 0:47
stefan999913-Jun-12 0:47 
QuestionGood code!! can these be used with multiple girds on page. Pin
Member 45166824-Jun-12 10:51
Member 45166824-Jun-12 10:51 
AnswerRe: Good code!! can these be used with multiple girds on page. Pin
Farooq Kaiser4-Jun-12 11:40
professionalFarooq Kaiser4-Jun-12 11:40 
GeneralRe: Good code!! can these be used with multiple girds on page. Pin
Member 1571175620-Jul-22 16:41
Member 1571175620-Jul-22 16:41 
AnswerRe: Good code!! can these be used with multiple girds on page. Pin
Member 1571175620-Jul-22 16:39
Member 1571175620-Jul-22 16:39 
QuestionGreat idea! 5 from me Pin
gstolarov24-Apr-12 7:49
gstolarov24-Apr-12 7:49 
GeneralMy vote of 3 Pin
FrewCen15-Apr-12 3:45
FrewCen15-Apr-12 3:45 
Use is good but too few features...
QuestionCan you attach source code Pin
demouser74313-Apr-12 19:12
demouser74313-Apr-12 19:12 

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.