Bind List to DataGridView : DataGridView « Database ADO.net « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Database ADO.net » DataGridViewScreenshots 
Bind List to DataGridView

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;

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 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(19701214)));
        people.Add(new Person("B", Sex.Male, new DateTime(19761029)));
        people.Add(new Person("J", Sex.Male, new DateTime(1945517)));
        people.Add(new Person("J", Sex.Female, new DateTime(198213)));

        dataGridView1.AutoGenerateColumns = true;
        dataGridView1.DataSource = people;
    }
    private void InitializeComponent() {
        this.dataGridView1 = new System.Windows.Forms.DataGridView();
        this.getData = new System.Windows.Forms.Button();
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
        this.SuspendLayout();
        // 
        this.dataGridView1.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.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.dataGridView1.Location = new System.Drawing.Point(1313);
        this.dataGridView1.Name = "dataGridView1";
        this.dataGridView1.Size = new System.Drawing.Size(267217);
        this.dataGridView1.TabIndex = 0;
        // 
        this.getData.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
        this.getData.Location = new System.Drawing.Point(205236);
        this.getData.Size = new System.Drawing.Size(7523);
        this.getData.Text = "Get Data";
        this.getData.UseVisualStyleBackColor = true;
        this.getData.Click += new System.EventHandler(this.getData_Click);

        this.AutoScaleDimensions = new System.Drawing.SizeF(6F13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(292271);
        this.Controls.Add(this.getData);
        this.Controls.Add(this.dataGridView1);
        this.Text = "DataSourceGenericCollection";
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
        this.ResumeLayout(false);
    }
    private System.Windows.Forms.DataGridView dataGridView1;
    private System.Windows.Forms.Button getData;

    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}


           
       
Related examples in the same category
1.Bind DataGridView to Array
2.Data Source with Generic Collection
3.Setup Columns for DataGridView
4.Custom DataGridView with DataGridViewTextBoxColumn, DataGridViewImageColumn,DataGridViewComboBoxColumn
5.Set up DataGridView from DataColumn
6.Use DataViewRowState to filter DataGridView
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.