FunctionX Practical Learning Logo

Simple Data Binding

 

Introduction

This is an example of simple data binding on a data grid.

Practical LearningPractical Learning: Creating the Database

  1. Create a new Windows Forms Application
  2. Place a DataGrid control on the form and resize it to use the most part of the form
     
  3. Double-click an empty area of the form and implement the file as follows:
     
    #pragma once
    
    
    namespace DataBinding2
    {
    	using namespace System;
    	using namespace System::ComponentModel;
    	using namespace System::Collections;
    	using namespace System::Windows::Forms;
    	using namespace System::Data;
    	using namespace System::Drawing;
    	using namespace System::Data::SqlClient;
    
    	/// <summary> 
    	/// Summary for Form1
    	///
    	/// WARNING: If you change the name of this class, you will need to change the 
    	///          'Resource File Name' property for the managed resource compiler tool 
    	///          associated with all .resx files this class depends on.  Otherwise,
    	///          the designers will not be able to interact properly with localized
    	///          resources associated with this form.
    	/// </summary>
    	public __gc class Form1 : public System::Windows::Forms::Form
    	{	
    	public:
    		Form1(void)
    		{
    			InitializeComponent();
    		}
      
    	protected:
    		void Dispose(Boolean disposing)
    		{
    			if (disposing && components)
    			{
    				components->Dispose();
    			}
    			__super::Dispose(disposing);
    		}
    	private: System::Windows::Forms::DataGrid *  dataGrid1;
    
    	private:
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		System::ComponentModel::Container * components;
    
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		void InitializeComponent(void)
    		{
    			System::Resources::ResourceManager *  resources = 
    new System::Resources::ResourceManager(__typeof(DataBinding2::Form1));
    			this->dataGrid1 = new System::Windows::Forms::DataGrid();
    (__try_cast<System::ComponentModel::ISupportInitialize *  >(this->dataGrid1))->BeginInit();
    			this->SuspendLayout();
    			// 
    			// dataGrid1
    			// 
    			this->dataGrid1->Anchor = (System::Windows::Forms::AnchorStyles)
    (((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom) 
    				| System::Windows::Forms::AnchorStyles::Left) 
    				| System::Windows::Forms::AnchorStyles::Right);
    		this->dataGrid1->DataMember = S"";
    	this->dataGrid1->HeaderForeColor = System::Drawing::SystemColors::ControlText;
    		this->dataGrid1->Location = System::Drawing::Point(16, 16);
    		this->dataGrid1->Name = S"dataGrid1";
    		this->dataGrid1->Size = System::Drawing::Size(360, 272);
    		this->dataGrid1->TabIndex = 0;
    		// 
    		// Form1
    		// 
    		this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
    		this->ClientSize = System::Drawing::Size(392, 301);
    		this->Controls->Add(this->dataGrid1);
    	this->Icon = (__try_cast<System::Drawing::Icon *  >(resources->GetObject(S"$this.Icon")));
    		this->Name = S"Form1";
    		this->Text = S"Simple Data Binding";
    		this->Load += new System::EventHandler(this, Form1_Load);
    	(__try_cast<System::ComponentModel::ISupportInitialize *  >(this->dataGrid1))->EndInit();
    		this->ResumeLayout(false);
    
    		}	
    	private: System::Void Form1_Load(System::Object *  sender, System::EventArgs *  e)
    			 {
    SqlConnection* sqlConn = 
    	new SqlConnection(S"server=localhost;trusted_connection=true;database=pubs");
    SqlDataAdapter* sqlA   = 
    	new SqlDataAdapter(S"SELECT emp_id, fname, lname, hire_date FROM employee",
    		sqlConn);
    				 DataSet* dsEmployees   = new DataSet;
    
    				 sqlA->Fill(dsEmployees, S"employee");
    				 this->dataGrid1->DataSource = dsEmployees;
    				 this->dataGrid1->DataMember = S"employee";
    			 }
    
    	};
    }
  4. Press Ctrl + F5 to test the application
     
  5. Close it

 

 

Home Copyright © 2004-2012, FunctionX