[★]一个非常简单的范型例子

  ==============================================================================
      private List<Gro> groceryList = new List<Gro>();
        private void frmMain_Load(object sender, EventArgs e)
        {
            Gro[] items = new Gro[]{
                new Gro("Yogurt",Convert.ToDecimal(2.95)),
                new Gro("Carrots",Convert.ToDecimal(1.55)),
                new Gro("Celery",Convert.ToDecimal(0.99)),
                new Gro("Rice",Convert.ToDecimal(5.39)),
                new Gro("Bread",Convert.ToDecimal(3.95)),
                new Gro("Milk",Convert.ToDecimal(3.95)),
                new Gro("Soda",Convert.ToDecimal(1.99)),
                new Gro("Cheese",Convert.ToDecimal(5.99)),
                new Gro("Paper Towels",Convert.ToDecimal(4.55))
            };

            groceryList.AddRange(items);

            UpdateGrid(groceryList);
        }
        private void UpdateGrid(List<Gro> list)
        {
            int c=1;
            decimal total=0;
            dataGridView1.Rows.Clear();
            list.ForEach(delegate(Gro item)
            {
                dataGridView1.Rows.Add(new object[] { c++, item.ITEM, String.Format("{0:c}",item.PRICE)});
                total += item.PRICE;
            });
            lblTotal.Text = String.Format("{0:c}",total);

         
        }
==
        class Gro
    {
        private string _item = null;
        private decimal _price = 0;

        public Gro(){}
        public Gro(string item, decimal price)
        {
            _item = item;
            _price = price;
        }

        public string ITEM
        {
            get { return this._item; }
        }

        public decimal PRICE
        {
            get { return this._price; }
        }
    }

你可能感兴趣的:(例子)