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(http://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 1 ASP.NET GridView And RepeatColumns(Rows in Multiple Columns) With jQuery

To create two columns layout, call following in javascript:

GridViewRepeatColumns("<%=gridSample.ClientID %>",2);
gridview 2 ASP.NET GridView And RepeatColumns(Rows in Multiple Columns) With jQuery

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

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

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

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

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.

Comments:  11

  • Prowshan

    What if gridview does not have header? In this project always header repeats on each column.

  • http://www.techbrij.com Brij Mohan

     Comment line 10 to 20, if gridview doesn’t have header.

  • Prabhatyadav3108

    thanku very much.. from Niki, Gaurang and Prabhat

  • http://www.techbrij.com Brij Mohan

    After grid declaration in aspx side, you can call the method OR you can use RegisterStartupScript method.

  • http://www.techbrij.com Brij Mohan

    After grid declaration aspx side, You can declare script tag and define the method and call it.

  • Shail

    How to add space between two columns ?

  • Kingsley RTMJ

    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!

  • swanand kulkarni

    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.

  • mukesh patil

    thanx You …my grid working good but first record of grid display duplicate record

  • Zahid Khan

    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.

  • Santosh Gandhi

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

    please help me