Mega Code Archive

 
Categories / C# / ADO Database
 

Data Source with Generic Collection

using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Text; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Windows.Forms; public class Person {     public Person(string name, Sex sex, DateTime dob) {         _name = name;         _sex = sex;         _dateOfBirth = dob;     }     public string Name {         get { return _name; }         set { _name = value; }     }     public Sex Sex {         get { return _sex; }         set { _sex = value; }     }     public DateTime DateOfBirth {         get { return _dateOfBirth; }         set { _dateOfBirth = value; }     }     private string _name;     private Sex _sex;     private DateTime _dateOfBirth; } public enum Sex {     Male,     Female } class PersonList : List<Person> { } public class SexDisplay {     public SexDisplay(Sex s) {         _sex = s;         _caption = s.ToString();     }     public Sex Sex {         get { return _sex; }     }     public string Caption {         get { return _caption; }     }     private Sex _sex;     private string _caption; } public class SexList : List<SexDisplay> { } class Form1 : Form {     public Form1() {         InitializeComponent();     }     private void getData_Click(object sender, EventArgs e) {         PersonList people = new PersonList();         people.Add(new Person("F", Sex.Male, new DateTime(1970, 12, 14)));         people.Add(new Person("B", Sex.Male, new DateTime(1976, 10, 29)));         people.Add(new Person("J", Sex.Male, new DateTime(1945, 5, 17)));         people.Add(new Person("J", Sex.Female, new DateTime(1982, 1, 3)));         dataGridView.AutoGenerateColumns = true;         dataGridView.DataSource = people;     }     private void Stuff() {         DataGridViewTextBoxColumn nameColumn = new DataGridViewTextBoxColumn();         nameColumn.DataPropertyName = "Name";         nameColumn.Name = "Name";         nameColumn.HeaderText = "Name";         nameColumn.ValueType = typeof(string);         dataGridView.Columns.Add(nameColumn);         DataGridViewButtonColumn buttonCol = new DataGridViewButtonColumn();         buttonCol.Text = "Hello";         buttonCol.Name = "Hello";         buttonCol.HeaderText = "Hello";         buttonCol.ValueType = typeof(string);         buttonCol.DataPropertyName = "Name";         dataGridView.Columns.Add(buttonCol);         DataGridViewCheckBoxColumn checkCol = new DataGridViewCheckBoxColumn();         checkCol.HeaderText = "Blah";         dataGridView.Columns.Add(checkCol);         DataGridViewComboBoxColumn comboCol = new DataGridViewComboBoxColumn();         comboCol.HeaderText = "Combo";         SexList sl = new SexList();         sl.Add(new SexDisplay(Sex.Male));         sl.Add(new SexDisplay(Sex.Female));         comboCol.DataSource = sl;         comboCol.DisplayMember = "Caption";         comboCol.ValueMember = "Sex";         comboCol.DataPropertyName = "Sex";         dataGridView.Columns.Add(comboCol);         DataGridViewLinkColumn linkCol = new DataGridViewLinkColumn();         linkCol.HeaderText = "Link";         linkCol.Text = "Bibble";         dataGridView.Columns.Add(linkCol);         linkCol.DataPropertyName = "DateOfBirth";     }     private void dataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e) {         int i = 0;     }     private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e) {         int i = 0;     }     private void InitializeComponent() {         this.getData = new System.Windows.Forms.Button();         this.dataGridView = new System.Windows.Forms.DataGridView();         this.SuspendLayout();         //          // getData         //          this.getData.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));         this.getData.Location = new System.Drawing.Point(776, 600);         this.getData.Name = "getData";         this.getData.TabIndex = 0;         this.getData.Text = "Get Data";         this.getData.Click += new System.EventHandler(this.getData_Click);         //          // dataGridView         //          this.dataGridView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)                     | System.Windows.Forms.AnchorStyles.Left)                     | System.Windows.Forms.AnchorStyles.Right)));         this.dataGridView.Location = new System.Drawing.Point(13, 13);         this.dataGridView.Name = "dataGridView";         this.dataGridView.Size = new System.Drawing.Size(837, 573);         this.dataGridView.TabIndex = 1;         this.dataGridView.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView_CellClick);         this.dataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.dataGridView_DataError);         //          // Form1         //          this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);         this.ClientSize = new System.Drawing.Size(863, 635);         this.Controls.Add(this.dataGridView);         this.Controls.Add(this.getData);         this.ResumeLayout(false);     }     private System.Windows.Forms.Button getData;     private System.Windows.Forms.DataGridView dataGridView;     [STAThread]     static void Main() {         Application.EnableVisualStyles();         Application.EnableRTLMirroring();         Application.Run(new Form1());     } }