Programmatic Data Display : DataGrid « ADO.Net « C# / CSharp Tutorial






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


   class ProgrammaticDataDisplay : Form
   {
      public ProgrammaticDataDisplay()
      {
         InitializeComponent();
         string connectionString = "server=localhost;Trusted_Connection=yes; database=northwind";
         string commandString = "Select CompanyName, ContactName, ContactTitle, Phone, Fax from Customers";

         SqlDataAdapter DataAdapter = new SqlDataAdapter( commandString, connectionString );
         DataSet DataSet = new DataSet();
         DataAdapter.Fill( DataSet, "Customers" );

         dataGridView1.DataSource = DataSet.Tables["Customers"].DefaultView;

      }
      private void InitializeComponent()
      {
         this.dataGridView1 = new System.Windows.Forms.DataGridView();
         ( ( System.ComponentModel.ISupportInitialize ) ( this.dataGridView1 ) ).BeginInit();
         this.SuspendLayout();
         this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
         this.dataGridView1.Location = new System.Drawing.Point( 0, 0 );
         this.dataGridView1.Name = "dataGridView1";
         this.dataGridView1.Size = new System.Drawing.Size( 579, 262 );
         this.dataGridView1.TabIndex = 0;
         this.AutoScaleBaseSize = new System.Drawing.Size( 5, 13 );
         this.ClientSize = new System.Drawing.Size( 579, 262 );
         this.Controls.Add( this.dataGridView1 );
         this.Name = "ProgrammaticDataDisplay";
         this.Text = "Programmatic Data Display";
         ( ( System.ComponentModel.ISupportInitialize ) ( this.dataGridView1 ) ).EndInit();
         this.ResumeLayout( false );

      }


      private System.Windows.Forms.DataGridView dataGridView1;

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








32.52.DataGrid
32.52.1.Load data in DataTable to DataGridLoad data in DataTable to DataGrid
32.52.2.Link two DataTable in a DataGridLink two DataTable in a DataGrid
32.52.3.Load Data to DataGridLoad Data to DataGrid
32.52.4.DataGrid View: on data error
32.52.5.Data binding for Multiple Controls
32.52.6.Binding DataSet to DataGrid
32.52.7.Programmatic Data Display