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






Bind DataGridView to Array


using System;
using System.Collections.Generic;
using System.ComponentModel;
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.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(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.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.Data Source with Generic Collection
2.Setup Columns for DataGridView
3.Bind List to DataGridView
4.Custom DataGridView with DataGridViewTextBoxColumn, DataGridViewImageColumn,DataGridViewComboBoxColumn
5.Set up DataGridView from DataColumn
6.Use DataViewRowState to filter DataGridView