Csharp/C Sharp by API/System.Windows.Forms/DataGridView

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

DataGridView.AutoGenerateColumns

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

using System;
using System.Collections.Generic;
using System.ruponentModel;
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(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)));
        dataGridView1.AutoGenerateColumns = true;
        dataGridView1.DataSource = people;
    }
    private void InitializeComponent() {
        this.dataGridView1 = new System.Windows.Forms.DataGridView();
        this.getData = new System.Windows.Forms.Button();
        ((System.ruponentModel.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(13, 13);
        this.dataGridView1.Name = "dataGridView1";
        this.dataGridView1.Size = new System.Drawing.Size(267, 217);
        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(205, 236);
        this.getData.Size = new System.Drawing.Size(75, 23);
        this.getData.Text = "Get Data";
        this.getData.UseVisualStyleBackColor = true;
        this.getData.Click += new System.EventHandler(this.getData_Click);
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(292, 271);
        this.Controls.Add(this.getData);
        this.Controls.Add(this.dataGridView1);
        this.Text = "DataSourceGenericCollection";
        ((System.ruponentModel.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());
    }
}


DataGridView.DataSource

  
using System.Drawing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
public class Book {
    public String Title { get; set; }
    public override String ToString() {
        return Title;
    }
}
public class FormBooks : Form {
    static public Book[] Books =
    {
      new Book {Title="F"},
      new Book {Title="B"}
    };
    public FormBooks() {
        InitializeComponent();
    }
    private void FormStrings_Load(object sender, EventArgs e) {
        String[] books = { "F", "A", "B", "R", "B" };
        var query =
          from book in books
          where book.Length > 10
          orderby book
          select new { Book = book.ToUpper() };
        dataGridView1.DataSource = query.ToList();
    }
    private void InitializeComponent() {
        this.dataGridView1 = new System.Windows.Forms.DataGridView();
        ((System.ruponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
        this.SuspendLayout();
        //
        // dataGridView1
        //
        this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.dataGridView1.Location = new System.Drawing.Point(10, 10);
        this.dataGridView1.Name = "dataGridView1";
        this.dataGridView1.Size = new System.Drawing.Size(272, 251);
        this.dataGridView1.TabIndex = 0;
        //
        // FormStrings
        //
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(292, 271);
        this.Controls.Add(this.dataGridView1);
        this.Name = "FormStrings";
        this.Padding = new System.Windows.Forms.Padding(10);
        this.Text = "FormStrings";
        this.Load += new System.EventHandler(this.FormStrings_Load);
        ((System.ruponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
        this.ResumeLayout(false);
    }
    private System.Windows.Forms.DataGridView dataGridView1;
    public static void Main() {
        Application.Run(new FormBooks());
    }
}


new DataGridView()

 

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
    public Form1() {
        InitializeComponent();
    }
    private void getData_Click(object sender, EventArgs e) {
        Item[] items = new Item[] { new Item ( "One" ) , new Item ( "Two" ) , new Item ( "Three" ) };
        dataGridView1.AutoGenerateColumns = true;
        dataGridView1.DataSource = items;
    }
    protected class Item {
        public Item(string text) {
            m_text = text;
        }
        public string Text {
            get { return m_text; }
        }
        private string m_text;
    }
    private void InitializeComponent() {
        this.dataGridView1 = new System.Windows.Forms.DataGridView();
        this.getData = new System.Windows.Forms.Button();
        ((System.ruponentModel.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(12, 12);
        this.dataGridView1.Name = "dataGridView1";
        this.dataGridView1.Size = new System.Drawing.Size(268, 223);
        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(204, 242);
        this.getData.Name = "getData";
        this.getData.Size = new System.Drawing.Size(75, 23);
        this.getData.TabIndex = 1;
        this.getData.Text = "Get Data";
        this.getData.UseVisualStyleBackColor = true;
        this.getData.Click += new System.EventHandler(this.getData_Click);
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(292, 271);
        this.Controls.Add(this.getData);
        this.Controls.Add(this.dataGridView1);
        ((System.ruponentModel.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());
    }
}