Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Resizable ASP.NET Gridview Columns using Jquery

0.00/5 (No votes)
14 Apr 2012 1  
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:

<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:

$(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.

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:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" <br />DataKeyNames="Id" GridLines="None" CssClass="GridViewStyle">

The final output can be seen here:

Summary

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here