Jun 3, 2012

ASP.NET GridView And RepeatColumns(Rows in Multiple Columns) With jQuery

By Default, There is no RepeatColumns property in ASP.NET Gridview to display rows in multiple columns as in DataList control. When Gridview has few columns, It doesn’t take full space of browser. To make good UI, You might need RepeatColumns property. This post explains how to display asp.net GridView rows in multiple columns using jQuery WITHOUT modifying any existing code.

Code:

function GridViewRepeatColumns(gridviewid, repeatColumns) {
            //Created By: Brij Mohan(https://techbrij.com)
            if (repeatColumns < 2) {
                alert('Invalid repeatColumns value');
                return;
            }
            var $gridview = $('#' + gridviewid);
            var $newTable = $('<table></table>');

            //Append first row in table
            var $firstRow =  $gridview.find('tr:eq(0)'),
            firstRowHTML = $firstRow.html(),
            colLength = $firstRow.children().length;

            $newTable.append($firstRow);

            //Append first row cells n times
            for (var i = 0; i < repeatColumns - 1; i++) {
                $newTable.find('tr:eq(0)').append(firstRowHTML);
            }

            while ($gridview.find('tr').length > 0) {
                var $gridRow = $gridview.find('tr:eq(0)');
               $newTable.append($gridRow);
               for (var i = 0; i < repeatColumns - 1; i++) {
                   if ($gridview.find('tr').length > 0) {
                       $gridRow.append($gridview.find('tr:eq(0)').html());
                       $gridview.find('tr:eq(0)').remove();
                   }
                   else {
                       for (var j = 0; j < colLength; j++) {
                           $gridRow.append('<td></td>');
                       }
                   }
               }
           }
            //update existing GridView
           $gridview.html($newTable.html());
        }

You have to use above method and pass gridviewid and number of repeated columns. suppose your gridview ID is gridSample and have following structure:

gridview jquery asp.net

To create two columns layout, call following in javascript:

GridViewRepeatColumns("<%=gridSample.ClientID %>",2);
gridview jquery asp.net

For Three columns layout:
GridViewRepeatColumns("<%=gridSample.ClientID %>",3);

gridview jquery asp.net

For Four columns layout:
GridViewRepeatColumns("<%=gridSample.ClientID %>",4);

gridview jquery asp.net

Now You see edit and delete functionality is as it is, No need to modify any other server side code.

Demo:

Here is the HTML rendered demo of 3 columns layout:

Hope, It saves your time.

17 comments

  1. Hey all – I did not see a version that supports vertically run columns so I modified the existing version to allow an additional variable to be passed in, “orientation” – “V” for vertical, anything else for horizontal. Here’s the revised function:

    function GridViewRepeatColumns(gridviewid, repeatColumns, orientation) {
    if (repeatColumns < 2) {
    alert('Invalid repeatColumns value = ');
    return;
    }
    var xrt = 0, xr = 0, currcol = 0;
    var $gridview = $('#' + gridviewid);
    var $newTable = $('’);
    //Append first row in table
    var $firstRow = $gridview.find(‘tr:eq(0)’),
    firstRowHTML = $firstRow.html(),
    colLength = $firstRow.children().length;
    $newTable.append($firstRow);

    xr = $gridview.find(‘tr’).length;
    xr = xr / repeatColumns;
    xrt = Math.floor(xr);
    if (xr > xrt) xrt++;
    // alert(‘New Grid rows need to be added (float) = ‘ + xr);
    // alert(‘New Grid rows need to be added (int) = ‘ + xrt);

    //Append first row cells n times
    for (var i = 0; i 0) {
    var $gridRow = $gridview.find(‘tr:eq(0)’);
    r++;
    $newTable.append($gridRow);
    for (var c = 1; c 0) {
    // alert(‘Adding cell ‘ + ((c * xrt) – (r * c)));
    $gridRow.append($gridview.find(‘tr:eq(‘ + ((c * xrt) – (r * c)) + ‘)’).html());
    $gridview.find(‘tr:eq(‘ + ((c * xrt) – (r * c)) + ‘)’).remove();
    }
    else {
    for (var j = 0; j < colLength; j++) {
    $gridRow.append('’);
    }
    }
    }
    }
    }
    else
    {
    while ($gridview.find(‘tr’).length > 0) {
    var $gridRow = $gridview.find(‘tr:eq(0)’);
    $newTable.append($gridRow);
    for (var i = 0; i 0) {
    $gridRow.append($gridview.find(‘tr:eq(0)’).html());
    $gridview.find(‘tr:eq(0)’).remove();
    }
    else {
    for (var j = 0; j < colLength; j++) {
    $gridRow.append('’);
    }
    }
    }
    }
    }
    //update existing GridView
    $gridview.html($newTable.html());
    }

  2. how to show data for a range like 1-10 in one column and 11-20 second column and so on………

    please help me

  3. Dear I used the method but it shows me error: Reference Error: GridiView1 is not define. Please help me how to implement it correctly. Thanks.

  4. This code is good for what I m looking for but I m not getting how to include this in our aspx page , so pls will u give some what brief explaination for this.

  5. Hi Brij, very good one!

    I am finding it hard to make it work on my code.

    Can you brief me how to use this in aspx page or how to call this from code behind?
    I am new to Jquery, sorry!

  6. Hi, this is very helpful but I cannot seem to get my script to run past the first few lines. I think it has something to do with where/how I am declaring the script type. Could you give me an example of how this would look inside an asp.net page? (i.e what the tag would look like and if I need anything else to run it successfully.

    Thank you very much!

Leave a Reply

Your email address will not be published. Required fields are marked *