December 22, 2014
Hot Topics:

Working with the DataGridView Control

  • December 28, 2007
  • By Bipin Joshi
  • Send Email »
  • More Articles »

The Customer class stores the database connection string in a static variable (strConn). You use a static variable because all the methods of the Customer class are static. Note that the strConn variable is initialized in a static constructor.

The GetDataSet() static method returns a DataSet filled with all the records from the Customers table. Inside, it uses an SqlDataAdapter instance to fill a DataSet. The name of the DataTable being created is supplied in the second parameter of the Fill() method (customer in above code).

The UpdateDataSet() static method accepts a DataSet whose changes are to be saved back to the database. Inside, it declares three SqlCommand objects, each representing INSERT, UPDATE, and DELETE queries respectively. Instead of specifying the CommandText and Connection properties, you use one of the constructors of the SqlCommand class that accepts the query to be executed and underlying connection. Your INSERT, UPDATE, and DELETE queries have one or more parameters. Hence, you declare three arrays of SqlParameter objects viz. pInsert, pUpdate, and pDelete. These parameters are added to the Parameters collection of SqlCommand objects by using the AddRange() method of the Parameters collection. Then, a new instance of the SqlDataAdapter class is created and its InsertCommand, UpdateCommand, and DeleteCommand properties are set to respective SqlCommand objects. To save the changes from DataSet back to the database, the Update() method of SqlDataAdapter class is called.

The GetCountries() method simply returns a DataSet filled with distinct country values from the Customers table. You need these countries later when creating your data entry form.

Simple Data Binding

Now that you have created a class that supplies the required data, create a Windows Form that displays it in a tabular form. Open the default Windows Form and drag and drop a DataGridView control on it. The DataGridView control is present on the Data node of the toolbox (see Figure 2).

Then, open the smart tag of the DataGridView control (see Figure 3) and select the "Edit Columns" option.

Doing so will open the "Edit Columns" dialog, as shown in Figure 4.



Click here for a larger image.

Click on the Add button to open the "Add Columns" dialog (see Figure 5).





Page 2 of 5



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel