The ASP. NET DataGrid Control presents a multi-column, fully templated grid. It is the most common and flexible control for all data binding Web controls in the. NET Framework. To some extent, the DataGrid user interface is similar to a Microsoft Excel worksheet. Although DataGrid has advanced programming interfaces and complete attribute sets, it only generates HTML tables that contain interleaved superlinks to provide interoperability functions such as sorting and paging commands ).
Using the DataGrid control, you can create a simple data binding column to display the data retrieved from the data source.) templated columns allow you to design the layout of cell content ), last but most importantly, the command-based column allows you to add specific features to the grid ).
The DataGrid Control is suitable for reporting data, and it is flexible enough to allow you to build complex and professional data tables that allow you to freely implement functions such as paging and sorting. However, other functions, such as deepening and master/detail, only require a small amount of work. In this month's column, I will discuss a feature that cannot be provided by the control itself, but is quite popular with many people. Therefore, let's take a look at how to automatically generate complex reports. In these tables, a summary row with partial aggregate must be displayed.
ASP. NET DataGrid Control
You can bind the actual data to the control instance by setting the DataSource attribute of the DataGrid Control. This attribute is of the general Object type and supports two configuration schemes. It is usually set using the data object that implements the ICollection interface. DataTable and DataView objects are usually used. Another method is to set it with another object type, such as a data reader object. However, in this case, you must enable the custom paging mechanism; otherwise, an exception occurs. To put it simply, you must either bind the DataGrid to a paging data source, that is, a set object used to implement the enumeration number) or provide Paging for yourself.
For Web controls, data binding is enabled. the user interface is refreshed only when the DataBind method is called. During the refresh process, the control traverses the data source and copies some rows to its Items collection. The Items attribute indicates the content of the currently displayed page. If the data source supports pagination (ICollection), The DataGrid selects the correct row subset from DataSource that fits the current page. Otherwise, it assumes that all the content of DataSource is suitable for the current page and all of them are loaded into Items. After filling in the Items, the user interface of the control is displayed.
What are the lessons learned here? All the content that the DataGrid Control can safely and consistently display is the rows contained in the bound data source. Therefore, if you want to insert a summary row to group some records and display partial totals by using the public key, you must specify the method to insert these summary rows directly to the data source.
However, inserting a summary row into a data source is not enough. In fact, we must be able to distinguish between summary and general, and present the former with different visual styles.
Before attaching data to the control, make sure that the data source contains all the summary rows required by the control. Next, hook the ItemCreated event, check each summary row, and draw them with different la S and styles. Let's take a look at how to insert summary rows between different SQL query rows. I will use a sample application based on the Northwind SQL Server database to explain my point of view. This application lists all orders that each customer has placed in a given year. Orders are grouped by year and customer ID. Each customer has an additional line to summarize the total and total orders.
The above describes the basic knowledge of the ASP. NET DataGrid Control.
- Comparison between ASP. NET's GridView and DataGrid controls
- . NET beginner's Guide: easily customize the DataGridView Control
- C # Some common operations on the DatagridView
- ASP.net exports the DataGrid content as an excel file
- Button selection event in the DataGrid