ata-bound controls play a key role in the development of ASP.NET applications. Data-driven controls allow you to associate their whole interface, or individual properties, with one or more columns of a .NET-compliant data source. In this article, I’ll delve into the depths of an extremely versatile data-bound control that is a fixed presence in any real-world ASP.NET application?the DataGrid control. I’ll focus on the key programming aspects of the control, including data binding, column mapping, paging, and sorting.
The DataGrid server control renders a multi-column, fully templated, data-bound grid and provides a highly customizable, Excel-like user interface. Bound to a DataGrid, the data source renders through an HTML table interspersed with HTML elements and hyperlinks. You can customize the contents of the cells in each column to some extent using system-provided, as well as, user-defined templates. The DataGrid supports various types of data-bound columns, including text, templated, and command columns. The data binding mechanism is nearly identical to that of other ASP.NET controls. You bind the data source using the DataSource property, but no data will actually load until you call the DataBind method.
You can set most of the DataGrid features declaratively in the page layout; the same set of attributes, though, are also available programmatically through the properties and methods of the DataGrid class.
Simple Data Binding
The following line of code is enough to enable grid functionality in a Web page. It declares a server-side instance of the DataGrid control and assigns it the name of grid. All of the code associated with the page, refers to the DataGrid using the specified ID. The ASP.NET runtime takes care of creating and initializing such an instance for each request.
Once you’ve placed the control onto the page, you bind it to the data source and display the resulting HTML code. The following code binds a DataTable to the grid.
void Page_Load(object sender, EventArgs e) { // Run the query and get // some data to display DataTable data = ExecuteQuery(cmdText, connString); // Bind the data to the grid grid.DataSource = data; grid.DataBind(); } DataTable ExecuteQuery(string cmdText, string connString) { SqlDataAdapter adapter; adapter = new SqlDataAdapter(cmdText, connString); DataTable data = new DataTable(); adapter.Fill(data); return data; }
Although effective in terms of retrieval and display, you probably would not use this code in real applications because as Figure 1 shows, the resulting DataGrid control doesn’t implement necessary features and the interface is too plain.
![]() |
The Programming Interface The DataGrid control has no method specific of the class. It inherits all the methods it provides (for example, DataBind) from the various base classes. The DataGrid inherits from the BaseDataList class and implements the INamingContainer interface. The BaseDataList abstract class inherits from WebControl and provides a common set of functionality for all data listing controls, including the DataList and the DataGrid. The INamingContainer interface is a marker interface that doesn’t require the implementation of any methods but only indicates that the class has some features. In particular, the interface marks the class as a naming container. A naming container is a class that provides an ID namespace for their child controls. Controls that act as naming containers guarantee that the ID attributes of their child controls are unique within the entire application?a key point for all controls that contain a subtree of controls. Table 1 details the list of properties supported by the DataGrid control, but doesn’t include the properties inherited by the BaseDataList and WebControl classes.
Many of the properties allow you to improve the grid’s look and feel. When setting attributes, you can either set the property individually at the
However, you can also define the e child node within the
The effect is the same, but the second option looks more elegant and is easier to maintain. Visual Studio .NET generates this type of code when you drop a DataGrid control on a Web form. The following schema illustrates the overall ASP.NET layout of the control.
The output of a DataGrid control consists of several constituent elements grouped in the ListItemType enumeration. (See Table 2 for more information.) Each element plays a clear role and has a precise location in the control’s user interface as Figure 2 shows.
|