========================================================== ==============================================
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 ;}
}
}