Calculation with DataGridView : 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.3.Calculation with DataGridView
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


   class SimpleSpreadForm : Form
   {
      public SimpleSpreadForm()
      {
         InitializeComponent();
         m_Grid.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
      }

      private void OnFormLoad(object sender, EventArgs e)
      {
         int start = (int)'A';
         for (int i = 0; i < 26; i++)
         {
            string colName = ((char)(i + start)).ToString();
            int index = m_Grid.Columns.Add(colName, colName);
            m_Grid.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
            m_Grid.Columns[i].Width = 75;
         }
         for (int i = 0; i < 50; i++)
         {
            m_Grid.Rows.Add();
         }
      }

      private void OnColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
      {
         m_Grid.ClearSelection();
         foreach (DataGridViewRow row in m_Grid.Rows)
         {
            row.Cells[e.ColumnIndex].Selected = true;
         }
      }

      private void OnRowAdded(object sender, DataGridViewRowsAddedEventArgs e)
      {
         m_Grid.Rows[e.RowIndex].HeaderCell.Value = e.RowIndex.ToString();
      }

      private void OnSumCells(object sender, EventArgs e)
      {
         DataGridViewSelectedCellCollection selCells = m_Grid.SelectedCells;
         if (selCells.Count < 2)
            return;

         Dictionary<int, int> rowSum = new Dictionary<int, int>();
         Dictionary<int, int> colSum = new Dictionary<int, int>();

         foreach (DataGridViewCell cell in selCells)
         {
            if (!rowSum.ContainsKey(cell.RowIndex))
            {
               if (cell.Value != null && cell.Value.ToString() != string.Empty)
               {
                  rowSum[cell.RowIndexint.Parse((string)cell.Value);
               }
            }
         }

      }
      private void InitializeComponent()
      {
         this.m_Grid = new System.Windows.Forms.DataGridView();
         this.toolStrip1 = new System.Windows.Forms.ToolStrip();
         this.m_SumToolStripButton = new System.Windows.Forms.ToolStripButton();
         ((System.ComponentModel.ISupportInitialize)(this.m_Grid)).BeginInit();
         this.toolStrip1.SuspendLayout();
         this.SuspendLayout();
         // 
         // m_Grid
         // 
         this.m_Grid.Dock = System.Windows.Forms.DockStyle.Fill;
         this.m_Grid.Location = new System.Drawing.Point(025);
         this.m_Grid.Name = "m_Grid";
         this.m_Grid.Size = new System.Drawing.Size(727467);
         this.m_Grid.TabIndex = 0;
         this.m_Grid.Text = "dataGridView1";
         this.m_Grid.ColumnHeaderMouseClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.OnColumnHeaderMouseClick);
         this.m_Grid.RowsAdded += new System.Windows.Forms.DataGridViewRowsAddedEventHandler(this.OnRowAdded);
         // 
         // toolStrip1
         // 
         this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.m_SumToolStripButton});
         this.toolStrip1.Location = new System.Drawing.Point(00);
         this.toolStrip1.Name = "toolStrip1";
         this.toolStrip1.Size = new System.Drawing.Size(72725);
         this.toolStrip1.TabIndex = 1;
         this.toolStrip1.Text = "toolStrip1";
         // 
         // m_SumToolStripButton
         // 
         this.m_SumToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
         this.m_SumToolStripButton.Name = "m_SumToolStripButton";
         this.m_SumToolStripButton.Text = "Sum";
         this.m_SumToolStripButton.Click += new System.EventHandler(this.OnSumCells);
         // 
         // SimpleSpreadForm
         // 
         this.AutoScaleDimensions = new System.Drawing.SizeF(6F13F);
         this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
         this.ClientSize = new System.Drawing.Size(727492);
         this.Controls.Add(this.m_Grid);
         this.Controls.Add(this.toolStrip1);
         this.Name = "SimpleSpreadForm";
         this.Text = "Form1";
         this.Load += new System.EventHandler(this.OnFormLoad);
         ((System.ComponentModel.ISupportInitialize)(this.m_Grid)).EndInit();
         this.toolStrip1.ResumeLayout(false);
         this.ResumeLayout(false);
         this.PerformLayout();

      }

      private System.Windows.Forms.DataGridView m_Grid;
      private System.Windows.Forms.ToolStrip toolStrip1;
      private System.Windows.Forms.ToolStripButton m_SumToolStripButton;

      [STAThread]
      static void Main()
      {
         Application.EnableVisualStyles();
         Application.Run(new SimpleSpreadForm());
      }
   }
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.