Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
This chapter is from the book

Using the Designer to Define Grids

Now that you understand how to code most of the common uses of the grid, let’s cover how to avoid having to write a lot of that code yourself. The DataGridView control supports a very rich experience while working in the Visual Studio designer through a combination of the designer Smart Tags, dialogs, and the Properties window.

For starters, if you have defined a data source in your project, you can simply drag a data collection source like a data table onto the form designer and a DataGridView instance will be created with all of its supporting objects. Additionally, the column definitions based on the grid’s data source properties let you set other properties, such as the AutoSizeMode, using the designer. If you select the grid and display its Smart Tag, as shown in Figure 6.3, you can modify the most common options of the grid’s appearance and behavior from there.


Figure 6.3 DataGridView Smart Tag

The Choose Data Source drop-down displays a data sources selection window similar to the one described in Chapter 5 for the Properties window. The presented data sources will be tailored to only those that implement the IList interface and thus are suitable for binding to the grid.

The Edit Columns and Add Column links display dialogs that let you define the columns that the grid will contain, shown in Figures 6.4 and 6.5 respectively.


Figure 6.4 Edit Columns Dialog


Figure 6.5 Add Column Dialog

The Edit Columns dialog lets you add and remove columns, set the order of the columns within the grid, and set all the design-time properties for a defined column in a focused dialog. The properties shown in the dialog will be tailored based on whether the column is a bound or unbound column, and will expose additional properties based on the column type and cell type. If you define custom column types and include them in your project, they will show up as options for new columns or for configuring columns through this dialog.

The Add Column dialog (see Figure 6.5) lets you add a new data-bound or unbound column to the grid. If you are adding a data-bound column, you can select from the columns available in the currently selected data source. You will first have to set the data source to an appropriate collection of data either through the Smart Tag or through the DataSource property in the Properties window. If you are adding an unbound column, then you just specify the name of the column, the type of the column, and the header text. When you click the Add button, the column is added to the grid, and the dialog remains open so you can quickly define multiple new columns.

Configuring the columns through these dialogs writes all the code for you that has been covered earlier in this chapter for defining columns and controlling their runtime behavior.

The Enable Adding check box on the DataGridView Smart Tag sets the AllowUserToAddRows property to true if checked, which displays a new empty row at the bottom of the grid. This lets users add a new row to the data collection by typing new values into the cells. The ability to support this depends on whether the grid is data bound, and, if so, whether the underlying object collection supports adding new items to the collection (see the discussion in Chapter 7). Likewise, the Enable Editing check box sets the ReadOnly property, which affects whether users can edit the contents of the grid in place, and Enable Deleting sets the AllowUserToDeleteRows property. The Enable Column Reordering check box sets the AllowUserToOrderColumns property, whose behavior is described in the next section.

The Dock in parent container link is only available if you first drag and drop a grid control onto a form. It does exactly what it says—it simply sets the Dock property to Fill.

In addition to the common properties and behaviors that you can configure through the Smart Tag, there are a bunch of other properties and events that you can configure at design time through the Properties window. Setting any of these properties generates appropriate code in the designer-generated partial class for the form in the InitializeComponent method. Most notably, you can configure any of the data-binding properties through the Properties window. You’ll probably want to set styles using the Properties window, because you can preview the results of those settings in the designer to make sure you are getting what you expect. Styles are discussed in more detail at the end of this chapter.

  • + Share This
  • 🔖 Save To Your Account