Thursday, 19 May 2016

Bind Class Property to GeidView Using C#.net In Asp.net

5/19/2016 - By Pranav Singh 0

This article will show you how you can bind a class property list to a gridview control using c#.net using asp.net.

So for this article first we will create a new asp.net application and add class file into the project. Here is the sample code into class file.

public class CountryDetail
    {
        public int Id { get; set; }
        public string CountryName { get; set; }
        public string Population { get; set; }
        public string Code { get; set; }
    }

After this we will add the below code of gridview into the page.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication7.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Bind Class Property to GeidView Using C#.net In Asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                <Columns>
                  <asp:BoundField DataField="Id" HeaderText="Id" />
                  <asp:BoundField DataField="CountryName" HeaderText="Country Name" />
                  <asp:BoundField DataField="Population" HeaderText="Population" />
                  <asp:BoundField DataField="Code" HeaderText="Code" />
                </Columns>
            </asp:GridView>
        </div>
    </form>
</body>
</html>

In above code I have bound the property of the class to the gridview. Now check the code to bind the data to gridview.

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication7
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            List<CountryDetail> countryDetail = new List<CountryDetail>();
            countryDetail = GetCountryData();
            GridView1.DataSource = countryDetail;
            GridView1.DataBind();
        }
        private List<CountryDetail> GetCountryData()
        {
            List<CountryDetail> countryDetail = new List<CountryDetail>();
            countryDetail.Add(new CountryDetail { Id = 1, CountryName = "India", Code = "IN", Population = "125Cr" });
            countryDetail.Add(new CountryDetail { Id = 2, CountryName = "Pakistan", Code = "PK", Population = "50Cr" });
            countryDetail.Add(new CountryDetail { Id = 3, CountryName = "America", Code = "US", Population = "24Cr" });
            return countryDetail;
        }

    }

In above code I have prepared the list of record. On page load I have assign the value to the gridview control to display the record into the gridview. 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

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