C# ADO Example

← All NMath Code Examples

 

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

using CenterSpace.NMath.Core;
using System.IO;

namespace CenterSpace.NMath.Examples.CSharp
{
  /// <summary>
  /// A .NET example in C# showing how to display a DataFrame in an ADO.NET DataGrid.
  /// </summary>
  /// <remarks>
  /// Changes to the grid are fed back into a new DataFrame. 
  /// Descriptive statistics are calculated and displayed next to the grid.
  /// </remarks>
  public class ADOExample : System.Windows.Forms.Form
  {
    private DataFrame data;

    private System.Windows.Forms.DataGrid dataGrid1;
    private System.Windows.Forms.Label title;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Label recordLongJumpLabel;
    private System.Windows.Forms.Label averageLongJumpLabel;
    private System.Windows.Forms.Label shortestLabel;
    private System.Windows.Forms.Label varianceLabel;
    private System.Windows.Forms.TextBox variance;
    private System.Windows.Forms.TextBox shortest;
    private System.Windows.Forms.TextBox record;
    private System.Windows.Forms.TextBox average;

    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;

    public ADOExample()
    {
      // Required for Windows Form Designer support
      InitializeComponent();

      // Data consists of high jump, discus throw, long jump marks set
      // during Olympic years from 1896 - 1984.
      data = DataFrame.Load( "ADOExample.dat", true, false, "\t", true );

      // Adjust year to 4 digits.  Eg. -4 => 1896.
      int yearColumn = data.IndexOfColumn( "Olympic_Year" );
      for ( int i = 0; i < data.Rows; i++ )
      {
        double original = (double) data[i, yearColumn];
        data[i, yearColumn] = original + 1900;
      }

      // Permute columns so year comes first.
      data.PermuteColumns( 1, 0 );

      // Convert my DataFrame to a DataTable.
      DataTable table = data.ToDataTable();

      // Remove the index column.
      table.Columns.Remove( data.RowKeyHeader );

      // Bind to DataGrid.
      dataGrid1.DataSource = table;

      Recompute();
    }

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
      if ( disposing )
      {
        if ( components != null )
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }

    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
      this.dataGrid1 = new System.Windows.Forms.DataGrid();
      this.title = new System.Windows.Forms.Label();
      this.button1 = new System.Windows.Forms.Button();
      this.recordLongJumpLabel = new System.Windows.Forms.Label();
      this.averageLongJumpLabel = new System.Windows.Forms.Label();
      this.record = new System.Windows.Forms.TextBox();
      this.average = new System.Windows.Forms.TextBox();
      this.shortestLabel = new System.Windows.Forms.Label();
      this.varianceLabel = new System.Windows.Forms.Label();
      this.variance = new System.Windows.Forms.TextBox();
      this.shortest = new System.Windows.Forms.TextBox();
      ( (System.ComponentModel.ISupportInitialize) ( this.dataGrid1 ) ).BeginInit();
      this.SuspendLayout();
      // 
      // dataGrid1
      // 
      this.dataGrid1.BackgroundColor = System.Drawing.Color.White;
      this.dataGrid1.CaptionVisible = false;
      this.dataGrid1.DataMember = "";
      this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
      this.dataGrid1.Location = new System.Drawing.Point( 24, 80 );
      this.dataGrid1.Name = "dataGrid1";
      this.dataGrid1.RowHeadersVisible = false;
      this.dataGrid1.Size = new System.Drawing.Size( 168, 312 );
      this.dataGrid1.TabIndex = 0;
      // 
      // title
      // 
      this.title.Font = new System.Drawing.Font( "Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( (System.Byte) ( 0 ) ) );
      this.title.Location = new System.Drawing.Point( 72, 16 );
      this.title.Name = "title";
      this.title.Size = new System.Drawing.Size( 272, 23 );
      this.title.TabIndex = 1;
      this.title.Text = "ADO Example";
      this.title.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
      // 
      // button1
      // 
      this.button1.Location = new System.Drawing.Point( 232, 320 );
      this.button1.Name = "button1";
      this.button1.Size = new System.Drawing.Size( 112, 24 );
      this.button1.TabIndex = 2;
      this.button1.Text = "Recompute";
      this.button1.Click += new System.EventHandler( this.RecomputeValues );
      // 
      // recordLongJumpLabel
      // 
      this.recordLongJumpLabel.Location = new System.Drawing.Point( 232, 96 );
      this.recordLongJumpLabel.Name = "recordLongJumpLabel";
      this.recordLongJumpLabel.Size = new System.Drawing.Size( 80, 32 );
      this.recordLongJumpLabel.TabIndex = 3;
      this.recordLongJumpLabel.Text = "Record";
      // 
      // averageLongJumpLabel
      // 
      this.averageLongJumpLabel.Location = new System.Drawing.Point( 232, 144 );
      this.averageLongJumpLabel.Name = "averageLongJumpLabel";
      this.averageLongJumpLabel.Size = new System.Drawing.Size( 56, 24 );
      this.averageLongJumpLabel.TabIndex = 4;
      this.averageLongJumpLabel.Text = "Average";
      // 
      // record
      // 
      this.record.Location = new System.Drawing.Point( 320, 96 );
      this.record.Name = "record";
      this.record.Size = new System.Drawing.Size( 72, 20 );
      this.record.TabIndex = 5;
      this.record.Text = "";
      // 
      // average
      // 
      this.average.Location = new System.Drawing.Point( 320, 144 );
      this.average.Name = "average";
      this.average.Size = new System.Drawing.Size( 72, 20 );
      this.average.TabIndex = 6;
      this.average.Text = "";
      // 
      // shortestLabel
      // 
      this.shortestLabel.Location = new System.Drawing.Point( 232, 192 );
      this.shortestLabel.Name = "shortestLabel";
      this.shortestLabel.Size = new System.Drawing.Size( 56, 16 );
      this.shortestLabel.TabIndex = 7;
      this.shortestLabel.Text = "Shortest";
      // 
      // varianceLabel
      // 
      this.varianceLabel.Location = new System.Drawing.Point( 232, 240 );
      this.varianceLabel.Name = "varianceLabel";
      this.varianceLabel.Size = new System.Drawing.Size( 72, 24 );
      this.varianceLabel.TabIndex = 8;
      this.varianceLabel.Text = "Variance";
      // 
      // variance
      // 
      this.variance.Location = new System.Drawing.Point( 320, 240 );
      this.variance.Name = "variance";
      this.variance.Size = new System.Drawing.Size( 72, 20 );
      this.variance.TabIndex = 9;
      this.variance.Text = "";
      // 
      // shortest
      // 
      this.shortest.Location = new System.Drawing.Point( 320, 192 );
      this.shortest.Name = "shortest";
      this.shortest.Size = new System.Drawing.Size( 72, 20 );
      this.shortest.TabIndex = 10;
      this.shortest.Text = "";
      // 
      // ADOExample
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size( 5, 13 );
      this.ClientSize = new System.Drawing.Size( 418, 416 );
      this.Controls.AddRange( new System.Windows.Forms.Control[] {
                                                                  this.shortest,
                                                                  this.variance,
                                                                  this.varianceLabel,
                                                                  this.shortestLabel,
                                                                  this.average,
                                                                  this.record,
                                                                  this.averageLongJumpLabel,
                                                                  this.recordLongJumpLabel,
                                                                  this.button1,
                                                                  this.title,
                                                                  this.dataGrid1} );
      this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
      this.Name = "ADOExample";
      this.Text = "ADO Example";
      ( (System.ComponentModel.ISupportInitialize) ( this.dataGrid1 ) ).EndInit();
      this.ResumeLayout( false );

    }
    #endregion

    static void Main()
    {
      Application.Run( new ADOExample() );
    }

    private void RecomputeValues( object sender, System.EventArgs e )
    {
      Recompute();
    }

    private void Recompute()
    {
      data = new DataFrame( (DataTable) dataGrid1.DataSource );
      average.Text = StatsFunctions.Mean( data["Long_Jump"] ).ToString();
      record.Text = StatsFunctions.MaxValue( data["Long_Jump"] ).ToString();
      shortest.Text = StatsFunctions.MinValue( data["Long_Jump"] ).ToString();
      variance.Text = StatsFunctions.Variance( data["Long_Jump"] ).ToString();
    }

  }  // class

}  // namespace


← All NMath Code Examples
Top