Home

DataGrid Binding

 

Introduction

The .NET Framework presents various database friendly features. Per haps one of its strengths is the tight relationship between Microsoft Visual Studio .NET and Microsoft SQL Server. This makes it particularly easy to access a SQL Server database from a Visual Studio .NET Windows application with very little effort. The job is handled with just a few steps:

  1. Create a connection to the server
  2. Create a data adapter that includes a command used to access the data from a database object such as a table or a view
  3. Create a DataSet object that would serve as the intermediary between the data in the database and the controls of a graphical application
  4. Fill the DataSet object with the data adapter

After performing these steps, you can then bind your Windows control(s) to the DataSet object.

Practical LearningPractical Learning: Binding Data to a DataGrid

  1. If you want to follow this lesson, start Microsoft Visual Studio .NET and create a Visual Basic Projects, a Visual C# Projects, a Visual J# Projects, or a Visual C++ .NET Projects as a Windows (Form) Application
  2. Name it DBinder1
  3. From the Windows Forms section of the Toolbox, click the DataGrid control and click the form
  4. Move and resize it to fit the form as much as possible:
     
  5. Double-click an empty area of the form to access its Load event
  6. Implement the event as follows:
     
    Visual C++ .NET
    namespace DBinder1
    {
    	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;
    
    
            . . . No Change
    
    
    System::Void Form1_Load(System::Object *  sender, System::EventArgs *  e)
    {
    	 SqlConnection *sqlConn = new SqlConnection(String::Concat(
    				S"Server=(local);trusted_connection=true;",
    				S"database=Northwind"));
     SqlDataAdapter *sqlDAP = new SqlDataAdapter(S"SELECT TitleOfCourtesy, FirstName, "
    			S"LastName, Title, HomePhone FROM Employees", sqlConn);
    	 DataSet *dsEmployees = new DataSet(S"Employees");
    	 sqlDAP->Fill(dsEmployees, S"Employees");
    	 dataGrid1->DataSource = dsEmployees->Tables->Item[S"Employees"];
    }
    Visual Basic .NET
    Imports System.Data
    Imports System.Data.SqlClient
    
    Public Class Form1
        Inherits System.Windows.Forms.Form
    
    
            . . . No Change
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    				Handles MyBase.Load
            Dim sqlConn As SqlConnection = New SqlConnection( _
                                                                    "Server=(local);trusted_connection=true;" & _
                                                                    "database=Northwind")
            Dim sqlDAP As SqlDataAdapter = New SqlDataAdapter( _
               "SELECT TitleOfCourtesy, FirstName, " & _
              "LastName, Title, HomePhone FROM Employees", sqlConn)
            Dim dsEmployees As DataSet = New DataSet("Employees")
            sqlDAP.Fill(dsEmployees, "Employees")
            dataGrid1.DataSource = dsEmployees.Tables("Employees")
        End Sub
    End Class
    
    Visual C#
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Data.SqlClient;
    
    
            . . . No Change
    
    
    private void Form1_Load(object sender, System.EventArgs e)
    {
    	SqlConnection sqlConn = new SqlConnection(
    				"Server=(local);trusted_connection=true;" +
    				"database=Northwind");
    SqlDataAdapter sqlDAP = new SqlDataAdapter("SELECT TitleOfCourtesy, FirstName, " +
    			"LastName, Title, HomePhone FROM Employees", sqlConn);
    	 DataSet dsEmployees = new DataSet("Employees");
    	 sqlDAP.Fill(dsEmployees, "Employees");
    	 dataGrid1.DataSource = dsEmployees.Tables["Employees"];
    }
    Visual J#
    package DBinder1;
    
    import System.Drawing.*;
    import System.Collections.*;
    import System.ComponentModel.*;
    import System.Windows.Forms.*;
    import System.Data.*;
    import System.Data.SqlClient.*;
    
    
    	. . . No Change
    
    
    	private void Form1_Load (Object sender, System.EventArgs e)
    	{
    		SqlConnection sqlConn = new SqlConnection(
    			"Server=(local);trusted_connection=true;" +
    			"database=Northwind");
    SqlDataAdapter sqlDAP = new SqlDataAdapter("SELECT TitleOfCourtesy, FirstName, " +
    			"LastName, Title, HomePhone FROM Employees", sqlConn);
    		DataSet dsEmployees = new DataSet("Employees");
    		sqlDAP.Fill(dsEmployees, "Employees");
    	dataGrid1.set_DataSource(dsEmployees.get_Tables().get_Item("Employees"));
    	}
    }
  7. Execute the application to test it
     
  8. Close the form
 

Home Copyright © 2005-2016, FunctionX