Advertisement
wingman007

C#_WinForms_DataBinding

Jan 24th, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.09 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace DataBindingTutorial
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         BindingSource bs = new BindingSource();
  16.  
  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.         }
  21.  
  22.         private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
  23.         {
  24.  
  25.         }
  26.  
  27.         private void Form1_Load(object sender, EventArgs e)
  28.         {
  29.             bs.DataSource = typeof(Airplane);                                //**
  30.             bs.Add(new Airplane("Boeing 747", 800));
  31.             bs.Add(new Airplane("Airbus A380", 1023));
  32.             bs.Add(new Airplane("Cessna 162", 67));
  33.  
  34.             grid.DataSource = bs;                                            //**
  35.             grid.AutoGenerateColumns = true; // create columns automatically //**
  36.             txtModel.DataBindings.Add("Text", bs, "Model");  
  37.         }
  38.     }
  39.  
  40.     class Airplane
  41.     {
  42.         public Airplane(string model, int fuelKg)
  43.         {
  44.             _id = ++lastID; Model = model; _fuelKg = fuelKg;
  45.         }
  46.         private static int lastID = 0;
  47.         public int _id;
  48.         public int ID { get { return _id; } }
  49.         public int _fuelKg;
  50.         public int FuelLeftKg { get { return _fuelKg; } set { _fuelKg = value; } }
  51.         public string _model;
  52.         public string Model { get { return _model; } set { _model = value; } }
  53.         public List<Passenger> _passengers = new List<Passenger>();
  54.         public List<Passenger> Passengers { get { return _passengers; } }
  55.     }
  56.  
  57.     class Passenger
  58.     {
  59.         public Passenger(string name)
  60.         {
  61.             _id = ++lastID; Name = name;
  62.         }
  63.         private static int lastID = 0;
  64.         public int _id;
  65.         public int ID { get { return _id; } }
  66.         public string _name;
  67.         public string Name { get { return _name; } set { _name = value; } }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement