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

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » GUI Windows Forms » DataGridView 
23.83.2.Programmatic Grid
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(0new 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(1313);
         this.m_AddColsToGridButton.Name = "m_AddColsToGridButton";
         this.m_AddColsToGridButton.Size = new System.Drawing.Size(13123);
         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(16813);
         this.m_AddMoreColsWithColCountButton.Name = "m_AddMoreColsWithColCountButton";
         this.m_AddMoreColsWithColCountButton.Size = new System.Drawing.Size(20823);
         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(39112);
         this.m_RemoveColsButton.Name = "m_RemoveColsButton";
         this.m_RemoveColsButton.Size = new System.Drawing.Size(19623);
         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(1285);
         this.m_Grid.Name = "m_Grid";
         this.m_Grid.Size = new System.Drawing.Size(574265);
         this.m_Grid.TabIndex = 3;
         // 
         // m_AddRowsButton
         // 
         this.m_AddRowsButton.Location = new System.Drawing.Point(1343);
         this.m_AddRowsButton.Name = "m_AddRowsButton";
         this.m_AddRowsButton.Size = new System.Drawing.Size(13123);
         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(16842);
         this.m_AddHeterows.Name = "m_AddHeterows";
         this.m_AddHeterows.Size = new System.Drawing.Size(13823);
         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(34641);
         this.m_AddAnotherHeterowButton.Name = "m_AddAnotherHeterowButton";
         this.m_AddAnotherHeterowButton.Size = new System.Drawing.Size(15923);
         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(6F13F);
         this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
         this.ClientSize = new System.Drawing.Size(650362);
         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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.