Wednesday, 23 July 2014

Frozen Rows and Columns in Asp.Net Mvc Webgrid Using jQuery Like Excel Sheet

7/23/2014 - By Pranav Singh 0

This article will demonstrate you how you can provide frozen rows and columns functionality like excel sheet in asp.net mvc application on webgrid using jQuery. This article you can use in MVC3, MVC4, MVC5.


So for this article first we will create a new asp.net mvc application. After creating this application add the model class in you project and add the below code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace FroozenRowAndColmn.Models
{
    public class StudentModel
    {
        public List<Student> StudentList { get; set; }
    }
    public class Student
    {
        public string Name { get; set; }
        public int Marks { get; set; }
        public string Class { get; set; }
        public string Section { get; set; }
        public string Address { get; set; }
        public int Rank { get; set; }
    }
}

After creating model we will create a controller and add the below code in your controller.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FroozenRowAndColmn.Models;

namespace FroozenRowAndColmn.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        public ActionResult Index()
        {
            StudentModel objstudentmodel = new StudentModel();
            objstudentmodel.StudentList = GetStudentRecord();
            return View(objstudentmodel);
        }
        ///
        /// This method is usd for creating record.
        /// You can use DB to access the data
        ///
        public List<Student> GetStudentRecord()
        {
            List<Student> _objstudent = new List<Student>();
            _objstudent.Add(new Student { Name = "Student 1", Class = "12th", Rank = 2, Section = "B", Address = "Address 1", Marks = 200 });
            _objstudent.Add(new Student { Name = "Student 2", Class = "12th", Rank = 5, Section = "A", Address = "Address 1", Marks = 200 });
            _objstudent.Add(new Student { Name = "Student 3", Class = "12th", Rank = 7, Section = "B", Address = "Address 1", Marks = 200 });
            _objstudent.Add(new Student { Name = "Student 4", Class = "12th", Rank = 4, Section = "C", Address = "Address 1", Marks = 200 });
            _objstudent.Add(new Student { Name = "Student 5", Class = "12th", Rank = 1, Section = "B", Address = "Address 1", Marks = 200 });
            _objstudent.Add(new Student { Name = "Student 6", Class = "12th", Rank = 8, Section = "A", Address = "Address 1", Marks = 200 });
            _objstudent.Add(new Student { Name = "Student 7", Class = "12th", Rank = 9, Section = "A", Address = "Address 1", Marks = 200 });
            _objstudent.Add(new Student { Name = "Student 8", Class = "12th", Rank = 21, Section = "D", Address = "Address 1", Marks = 200 });
            _objstudent.Add(new Student { Name = "Student 9", Class = "12th", Rank = 12, Section = "C", Address = "Address 1", Marks = 200 });
            _objstudent.Add(new Student { Name = "Student 10", Class = "12th", Rank = 4, Section = "C", Address = "Address 1", Marks = 200 });
            _objstudent.Add(new Student { Name = "Student 11", Class = "12th", Rank = 2, Section = "A", Address = "Address 1", Marks = 200 });
            _objstudent.Add(new Student { Name = "Student 12", Class = "12th", Rank = 12, Section = "B", Address = "Address 1", Marks = 200 });
            _objstudent.Add(new Student { Name = "Student 13", Class = "12th", Rank = 10, Section = "B", Address = "Address 1", Marks = 200 });
            _objstudent.Add(new Student { Name = "Student 14", Class = "12th", Rank = 23, Section = "C", Address = "Address 1", Marks = 200 });
            _objstudent.Add(new Student { Name = "Student 15", Class = "12th", Rank = 20, Section = "B", Address = "Address 1", Marks = 200 });
            _objstudent.Add(new Student { Name = "Student 16", Class = "12th", Rank = 21, Section = "A", Address = "Address 1", Marks = 200 });
            _objstudent.Add(new Student { Name = "Student 17", Class = "12th", Rank = 22, Section = "A", Address = "Address 1", Marks = 200 });
            return _objstudent;
        }
    }
}
      
Now we will create view for the controller action. In your view add the below code.

@model FroozenRowAndColmn.Models.StudentModel
@{
    ViewBag.Title = "Frozen Rows and Columns in Asp.Net Mvc Webgrid Using jQuery Like Excel";
}
<script src="../../Script/jquery.js" type="text/javascript"></script>
<script src="../../Script/jquery.dataTables.js" type="text/javascript"></script>
<script src="../../Script/FixedColumns.js" type="text/javascript"></script>
<script  type="text/javascript">
    $(document).ready(function () {
        var oTable = $('#studentgrid').dataTable({
            "sScrollX": "0",
            "sScrollY": "200px",
            "sScrollXInner": "140%",
            "bPaginate": false,
            "bLengthChange": false,
            "bFilter": false,
            "bSort": false,
            "bInfo": false,
            "bAutoWidth": false         });
        new FixedColumns(oTable, {
            "iLeftColumns": 2,
            "iLeftWidth": 200
        });
    });       
</script>
<style type="text/css">
    table
    {
        border-collapse: collapse;
    }
   
    td
    {
        border: 1px solid #999;
        padding: 0.3rem;
        text-align: left;
    }
    th
    {
        border: 1px solid #999;
        background-color:Gray;
        padding: 0.3rem;
        text-align: left;
    }
</style>
@{
    var grid = new WebGrid(source: Model.StudentList,
                               rowsPerPage: 100, canSort: false);
                      
    @grid.GetHtml(
                tableStyle: "table",
                headerStyle: "header",
                footerStyle: "footer",
                htmlAttributes: new { id = "studentgrid" },
    columns: grid.Columns(
                grid.Column("Name", "Name"),
                grid.Column("Class", "Class"),
                grid.Column("Rank", "Rank"),
                grid.Column("Section", "Section"),
                grid.Column("Address", "Address"),
                grid.Column("Marks", "Marks"))
                    )
               
}

In above code first we have added the js reference.

<script src="../../Script/jquery.js" type="text/javascript"></script>
<script src="../../Script/jquery.dataTables.js" type="text/javascript"></script>
<script src="../../Script/FixedColumns.js" type="text/javascript"></script>

Now below mention js code is used for freezing the rows and columns.

<script  type="text/javascript">
    $(document).ready(function () {
        var oTable = $('#studentgrid').dataTable({
            "sScrollX": "0",
            "sScrollY": "200px",
            "sScrollXInner": "140%",
            "bPaginate": false,
            "bLengthChange": false,
            "bFilter": false,
            "bSort": false,
            "bInfo": false,
            "bAutoWidth": false         });
        new FixedColumns(oTable, {
            "iLeftColumns": 2,
            "iLeftWidth": 200
        });
    });       
</script>

In above code just check the FixedColumns. In this we will define the left side no of columns to freeze and width of frozen columns. By making change in this section you can change the no of columns to be freeze.

Now we have done run the application to check the output.


DOWNLOAD

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top