Wednesday 26 July 2017

How To Bind The Data To List And Get The Data From List In C#

In this article i would like to know you about how the data should be bind to list and get the data from the List using c# code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConvertJSONDataToCSharpObject
{
    class Program
    {
        static void Main(string[] args)
        {
            ItemsDataList itemLst = new ItemsDataList();
            var itmList= itemLst.ItemList;
            foreach(var item in itmList)
            {
                Console.WriteLine("Status: " + item.ItemId +" ");
                Console.WriteLine("Description: " + item.ItemName+" ");
                Console.WriteLine("Definition: " + item.ItemDescription+" ");
                Console.WriteLine();
            }
            Console.Read();
        }
    }

    // Class ItemDetails
    public class ItemDetails
    {
        public string ItemId { get; set; }
        public string ItemName { get; set; }
        public string ItemDescription { get; set; }
    }

 // The Below class is having property named 'ItemList' with get; set; accessors
    public class ItemsDataList
    {
       //  To get data from list
        public List<ItemDetails> ItemList
        {
            get { return itemList; }
        }

        //  Binding data to List i.e., set data to list
        List<ItemDetails> itemList = new List<ItemDetails>()
        {
           new ItemDetails() {
               ItemId ="1",
               ItemName ="KeyBoard",
               ItemDescription ="Generic 108 Keys PC And Laptop Black"
           },
           new ItemDetails() {
               ItemId ="2",
               ItemName ="Mouse",
               ItemDescription ="Generic Wireless USB  Optical Mouse for Laptop And PC"
           },
           new ItemDetails() {
               ItemId ="3",
               ItemName ="Pendrive",
               ItemDescription ="16GB USB 2.0 Pen Drive"
           },
            new ItemDetails() {
               ItemId ="4",
               ItemName ="DVD Drive",
               ItemDescription ="USB 2.0 External DVD Drive"
           },

    };


    }


}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.