Bind Data to DataGridView in Window Form Application

Create a Window form application have a look at my previous blog - how to create window form application. Once our window is ready click on Toolbox under “Data” choose “DataGridView” and drag onto the window. 

 

We will have,

 
Here we are binding the data from database table. Double click on window so we will get the window load method.
 
  

Now add new classes file to the project

 

Once class is created add the database connection method and method to retrieve data. 

  1. public static class DatabaseInterface    
  2. {    
  3.     static MySqlConnection databaseConnection = null;    
  4.   
  5.     /// <summary>    
  6.     /// Provides database connection    
  7.     /// </summary>    
  8.     /// <returns></returns>    
  9.     public static MySqlConnection getDBConnection()    
  10.     {    
  11.         string connectionString = ConfigurationManager.ConnectionStrings["myDemoConnection"].ConnectionString;    
  12.         databaseConnection = new MySqlConnection(connectionString);    
  13.         return databaseConnection;    
  14.     }// Database Connection    
  15.   
  16.   
  17.     /// <summary>    
  18.     /// Retrive data from table    
  19.     /// </summary>    
  20.     /// <returns></returns>    
  21.     public static DataTable getData()    
  22.     {    
  23.         try    
  24.         {    
  25.             DataTable countries = new DataTable();    
  26.             DataSet DsConType = new DataSet();    
  27.             string sqlStatement = "SELECT * FROM country ORDER BY id ASC";    
  28.             getDBConnection().Open();    
  29.             MySqlDataAdapter myDatabaseAdapter = new MySqlDataAdapter(sqlStatement, databaseConnection);    
  30.             myDatabaseAdapter.Fill(DsConType, "AllCountries");    
  31.             return DsConType.Tables["AllCountries"];    
  32.         }    
  33.         catch (Exception excp)    
  34.         {    
  35.             throw excp;    
  36.         }    
  37.         finally    
  38.         {    
  39.             if (databaseConnection != null)    
  40.             {    
  41.                 databaseConnection.Close();    
  42.             }    
  43.         }    
  44.   
  45.     }// getData    
  46. }   

 

Here I used “ConfigurationManager” to retrieve the connection string from app.config. to know how to connect through app.config please follow the my revious blog. Once it is finished then we have to bind get the table info in window _load() and bind it to “DataGridView”. In this case I have renamed Grid View as “dataGridViewCountry”.
  1. private void MainWindow_Load(object sender, EventArgs e)      
  2. {      
  3.     try      
  4.     {      
  5.         DataTable countryTable = new DataTable();      
  6.         countryTable = DatabaseInterface.getData();      
  7.     
  8.         BindingSource primaryGroupsBinding = new BindingSource();      
  9.         primaryGroupsBinding.DataSource = countryTable;      
  10.         dataGridViewCountry.DataSource = primaryGroupsBinding;      
  11.     }      
  12.     catch(Exception ex)      
  13.     {      
  14.         MessageBox.Show(ex.Message,"Error");      
  15.     }                  
  16. }   
Now our “DataGridView” is ready with data.