January 20, 2021
Hot Topics:

Working with the DataGridView Control

  • By Bipin Joshi
  • Send Email »
  • More Articles »

The "Add Columns" dialog allows you to add a new column to the DataGridView control. While adding a column, you can specify its programmatic Name, column heading (Header Text), and its type (Textbox, Button, ComboBox, and so forth). These properties can be changed even after adding the column. Add in all four textbox columns to the grid for showing CustomerID, CompanyName, ContactName, and Country columns, respectively. Then, set the DataPropertyName property of each of the columns to CustomerID, CompanyName, ContactName, and Country, respectively. The DataPropertyName property indicates the name of a column from the underlying data source that is to displayed in the column of the grid.

After configuring the columns, go to the Load event handler of the form and write the following code.

private void Form1_Load(object sender, EventArgs e)
   dataGridView1.DataSource = Customer.GetDataSet();
   dataGridView1.DataMember = "customers";

The above code sets the DataSource property of the DataGridView control to the DataSet returned by GetDataSet() method. The DataSource property can specify any type of "collection" such as DataSet, DataTable, ArrayList, or generic List. If your data source is a DataSet, you must specify the DataMember property also. The DataMember property specifies the name of the DataTable from the DataSet that is to be bound with the grid.

Whatever code you have written up till now will take care of displaying the data in the DataGridView. However, any changes made to the data are made to the DataSet only. To save these changes back to the database, you must call UpdateDataSet() method of the Customer class. To call this method, add a Button control below the DataGridView control and write the following code in its Click event handler.

private void button1_Click(object sender, EventArgs e)

Here, you simply call the UpdateDataSet() method. Notice that you need to typecast the object specified by DataSource property to the appropriate type.

Run the application and you should see the DataGridView control filled with the data (see Figure 6).

Click here for a larger image.

Notice that You can add, edit, and delete data from the grid without writing any specific code.

Using Button and ComboBox Columns

Although your grid is functional and allows you to add, edit, and delete data, it would be nice if you could:

  • Show a list of acceptable countries in the Country column from which the user can pick the value.
  • Provide a Delete button for each row. Because the built-in delete functionality is not very obvious (you need to click in the left hand side margin and then hit the Delete button), some users may find it confusing.

Fortunately, the DataGridView control comes with a set of predefined column types. The six types of columns supported by the DataGridView control are:

  • DataGridViewTextBoxColumn: Displays a Textbox in each cell of the column for editing the values
  • DataGridViewButtonColumn: Displays a push button in each cell of the column
  • DataGridViewCheckBoxColumn: Displays a checkbox in each cell of the column
  • DataGridViewComboBoxColumn: Displays a combo box in each cell of the column
  • DataGridViewImageColumn: Displays an image in each cell of the column
  • DataGridViewLinkColumn: Displays a link button in each cell of the column

To see these column types in action, you will modify your preceding example. Open the "Edit Columns" dialog of the DataGridView control and delete the existing Country column. Then, add a new DataGridViewComboBoxColumn column to the grid and set its Header Text to Country. Finally, add a new DataGridViewButtonColumn. Then, set the DataPropertyName property of the Country combo box column to Country. Also, set the Text property of the button column to Delete and UseColumnTextForButtonValue property to True. The UseColumnTextForButtonValue property governs if the value indicated by the Text property will be used as the button caption.

Now, modify the Load event handler of the form as shown below:

private void Form1_Load(object sender, EventArgs e)
   DataGridViewComboBoxColumn lst=
   lst.DataSource    = Customer.GetCountries().Tables[0];
   lst.DisplayMember = "Country";
   lst.ValueMember   = "Country";
   dataGridView1.DataSource = Customer.GetDataSet();
   dataGridView1.DataMember = "customers";

The above code first gets a reference to the combo box column and sets its DataSource property to the countries DataTable. The DisplayMember property specifies the column name to be displayed in the combobox. Similarly, the ValueMember property specifies the column name whose value is to be accessed programmatically. Finally, the DataGridView is bound with the DataSet as before.

Page 3 of 5

This article was originally published on December 28, 2007

Enterprise Development Update

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

Thanks for your registration, follow us on our social networks to keep up-to-date