Programmatic Grid : DataGridView « GUI Windows Forms « C# / CSharp Tutorial






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


   class ProgrammaticGridForm: Form
   {
      public ProgrammaticGridForm()
      {
         InitializeComponent();
      }

      private void OnAddCols(object sender, EventArgs e)
      {
         m_Grid.Columns.Add("MyColumnName", "MyColumnHeaderText");
         m_Grid.Columns.Add("MyOtherColumnName", "MyOtherColumnHeaderText");
      }

      private void OnAddMore(object sender, EventArgs e)
      {
         m_Grid.ColumnCount = 5;
         m_Grid.Columns[2].Name = "Col3";
         m_Grid.Columns[2].HeaderText = "Col3";
         m_Grid.Columns[3].Name = "Col4";
         m_Grid.Columns[3].HeaderText = "Col4";
         m_Grid.Columns[4].Name = "Col5";
         m_Grid.Columns[4].HeaderText = "Col5";
      }

      private void OnRemove(object sender, EventArgs e)
      {
         m_Grid.ColumnCount = 3;
      }

      private void OnAddRows(object sender, EventArgs e)
      {
         m_Grid.Rows.Add(20);
      }

      private void OnAddHeterows(object sender, EventArgs e)
      {
         m_Grid.ColumnCount = 5;
         DataGridViewRow heterow = new DataGridViewRow();
         DataGridViewComboBoxCell comboCell = new DataGridViewComboBoxCell();
         comboCell.Items.Add("Black");
         comboCell.Items.Add("White");
         comboCell.Value = "White";
         heterow.Cells.Add(comboCell);
         for (int i = 0; i < 4; i++)
         {
            heterow.Cells.Add(new DataGridViewTextBoxCell());
         }
         m_Grid.Rows.Add(heterow);

      }

      private void OnAddAnotherHeterow(object sender, EventArgs e)
      {
         DataGridViewRow heterow = new DataGridViewRow();
         heterow.CreateCells(m_Grid);
         heterow.Cells.RemoveAt(0);
         heterow.Cells.Insert(0, new DataGridViewComboBoxCell());
         m_Grid.Rows.Add(heterow);

      }

      private void InitializeComponent()
      {
         this.m_AddColsToGridButton = new System.Windows.Forms.Button();
         this.m_AddMoreColsWithColCountButton = new System.Windows.Forms.Button();
         this.m_RemoveColsButton = new System.Windows.Forms.Button();
         this.m_Grid = new System.Windows.Forms.DataGridView();
         this.m_AddRowsButton = new System.Windows.Forms.Button();
         this.m_AddHeterows = new System.Windows.Forms.Button();
         this.m_AddAnotherHeterowButton = new System.Windows.Forms.Button();
         ((System.ComponentModel.ISupportInitialize)(this.m_Grid)).BeginInit();
         this.SuspendLayout();
         // 
         // m_AddColsToGridButton
         // 
         this.m_AddColsToGridButton.Location = new System.Drawing.Point(13, 13);
         this.m_AddColsToGridButton.Name = "m_AddColsToGridButton";
         this.m_AddColsToGridButton.Size = new System.Drawing.Size(131, 23);
         this.m_AddColsToGridButton.TabIndex = 0;
         this.m_AddColsToGridButton.Text = "Add Columns To Grid";
         this.m_AddColsToGridButton.Click += new System.EventHandler(this.OnAddCols);
         // 
         // m_AddMoreColsWithColCountButton
         // 
         this.m_AddMoreColsWithColCountButton.Location = new System.Drawing.Point(168, 13);
         this.m_AddMoreColsWithColCountButton.Name = "m_AddMoreColsWithColCountButton";
         this.m_AddMoreColsWithColCountButton.Size = new System.Drawing.Size(208, 23);
         this.m_AddMoreColsWithColCountButton.TabIndex = 1;
         this.m_AddMoreColsWithColCountButton.Text = "Add More Columns with ColumnCount";
         this.m_AddMoreColsWithColCountButton.Click += new System.EventHandler(this.OnAddMore);
         // 
         // m_RemoveColsButton
         // 
         this.m_RemoveColsButton.Location = new System.Drawing.Point(391, 12);
         this.m_RemoveColsButton.Name = "m_RemoveColsButton";
         this.m_RemoveColsButton.Size = new System.Drawing.Size(196, 23);
         this.m_RemoveColsButton.TabIndex = 2;
         this.m_RemoveColsButton.Text = "Remove Columns with ColumnCount";
         this.m_RemoveColsButton.Click += new System.EventHandler(this.OnRemove);
         // 
         // m_Grid
         // 
         this.m_Grid.Location = new System.Drawing.Point(12, 85);
         this.m_Grid.Name = "m_Grid";
         this.m_Grid.Size = new System.Drawing.Size(574, 265);
         this.m_Grid.TabIndex = 3;
         // 
         // m_AddRowsButton
         // 
         this.m_AddRowsButton.Location = new System.Drawing.Point(13, 43);
         this.m_AddRowsButton.Name = "m_AddRowsButton";
         this.m_AddRowsButton.Size = new System.Drawing.Size(131, 23);
         this.m_AddRowsButton.TabIndex = 4;
         this.m_AddRowsButton.Text = "Add Rows";
         this.m_AddRowsButton.Click += new System.EventHandler(this.OnAddRows);
         // 
         // m_AddHeterows
         // 
         this.m_AddHeterows.Location = new System.Drawing.Point(168, 42);
         this.m_AddHeterows.Name = "m_AddHeterows";
         this.m_AddHeterows.Size = new System.Drawing.Size(138, 23);
         this.m_AddHeterows.TabIndex = 5;
         this.m_AddHeterows.Text = "Add Heterows";
         this.m_AddHeterows.Click += new System.EventHandler(this.OnAddHeterows);
         // 
         // m_AddAnotherHeterowButton
         // 
         this.m_AddAnotherHeterowButton.Location = new System.Drawing.Point(346, 41);
         this.m_AddAnotherHeterowButton.Name = "m_AddAnotherHeterowButton";
         this.m_AddAnotherHeterowButton.Size = new System.Drawing.Size(159, 23);
         this.m_AddAnotherHeterowButton.TabIndex = 6;
         this.m_AddAnotherHeterowButton.Text = "Add Another Heterow";
         this.m_AddAnotherHeterowButton.Click += new System.EventHandler(this.OnAddAnotherHeterow);
         // 
         // ProgrammaticGridForm
         // 
         this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
         this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
         this.ClientSize = new System.Drawing.Size(650, 362);
         this.Controls.Add(this.m_AddAnotherHeterowButton);
         this.Controls.Add(this.m_AddHeterows);
         this.Controls.Add(this.m_AddRowsButton);
         this.Controls.Add(this.m_Grid);
         this.Controls.Add(this.m_RemoveColsButton);
         this.Controls.Add(this.m_AddMoreColsWithColCountButton);
         this.Controls.Add(this.m_AddColsToGridButton);
         this.Name = "ProgrammaticGridForm";
         this.Text = "Programmatic Grid";
         ((System.ComponentModel.ISupportInitialize)(this.m_Grid)).EndInit();
         this.ResumeLayout(false);

      }


      private System.Windows.Forms.Button m_AddColsToGridButton;
      private System.Windows.Forms.Button m_AddMoreColsWithColCountButton;
      private System.Windows.Forms.Button m_RemoveColsButton;
      private System.Windows.Forms.DataGridView m_Grid;
      private System.Windows.Forms.Button m_AddRowsButton;
      private System.Windows.Forms.Button m_AddHeterows;
      private System.Windows.Forms.Button m_AddAnotherHeterowButton;

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








23.83.DataGridView
23.83.1.Simple DataGridView
23.83.2.Programmatic Grid
23.83.3.Calculation with DataGridView
23.83.4.Custom Header Cells
23.83.5.Fill Columns
23.83.6.Virtual Data