connectseekers.com

Friday, 26 May 2017

Asp.Net MVC : WebGrid Image Binding With Shorting Data Using C#.Net

5/26/2017 - By Pranav Singh 0

This article will show you how you can create a web grid data bind in asp.net mvc using c# with image bind and shorting feature of web grid on header click.

So for this article first we will create a new asp.net application and add a model class file. After this model class file we will add the below model class code.

public class CountryModel
    {
        public List<Country> CountryList { get; set; }
    }
    public class Country
    {
        public string CountryName { get; set; }
        public int Population { get; set; }
        public string FlagURL { get; set; }
    }

This code we used for providing the model class collection of data. Now we will create the model controller class file. Ad add the below code.

[HttpGet]
        public ActionResult Index()
        {
            CountryModel _countryModel = new CountryModel();
            _countryModel.CountryList= GetCountryList();
            return View(_countryModel);
        }

        ///

        /// Country list
         ///
        public List<Country> GetCountryList()
        {
            List<Country> _country = new List<Country>();
            _country.Add(new Country { CountryName = "India", Population = 124000, FlagURL = "CountryFlag/india.png" });
            _country.Add(new Country { CountryName = "China", Population = 84545, FlagURL = "CountryFlag/china.png" });
            _country.Add(new Country { CountryName = "Germany", Population = 9354999, FlagURL = "CountryFlag/germany.png" });
            _country.Add(new Country { CountryName = "Japan", Population = 24999, FlagURL = "CountryFlag/japan.png" });
            return _country;
        }

      
Hear in above code I have created a function which will provide data to the model and and return to the view for displaying it. Now we will create a view and add the below code.

@model MVC_Demos.Models.CountryModel
@{
    ViewBag.Title = "Asp.Net MVC : WebGrid Image Binding With Shorting Data Using C#.Net";
}

<style type="text/css">
    table.gridtable {
        font-family: verdana,arial,sans-serif;
        font-size: 11px;
        color: #333333;
        border-width: 1px;
        border-color: #666666;
        border-collapse: collapse;
    }

        table.gridtable th {
            border-width: 1px;
            padding: 8px;
            border-style: solid;
            border-color: #666666;
            background-color: #dedede;
        }

        table.gridtable td {
            border-width: 1px;
            padding: 8px;
            border-style: solid;
            border-color: #666666;
            background-color: #ffffff;
        }
</style>

@using (Html.BeginForm("Index", "Home"))
{
    <table width="100%" cellpadding="5" cellspacing="2" border="0" style="background-color: White;">
        <tr>
            <td>
                @{
    var grid = new WebGrid(source: Model.CountryList,
    rowsPerPage: 10,
    canSort: true);                                                                     

                }
                @grid.GetHtml(
                    tableStyle: "gridtable",
                    alternatingRowStyle: "even",
                    columns: grid.Columns(
                    grid.Column("CountryName", "CountryName"),
                    grid.Column("Population", "Population"),
                    grid.Column("FlagURL", header: "Country Flag", format: @<img src="~/@item.FlagURL" alt="@item.CountryName" />
)
         ))
            </td>
        </tr>
    </table>

}

In above code please check the highlighted part of the code. This piece of code is responsible for shorting the grid data by clicking on header of the grid. Now we have done run the application to check the output.



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
Email Id : contact@aspdotnet-pools.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


Top jQuery Plugins

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