Section 2: Customize the appearance of the DataGrid
In the previous article, I initially felt the convenience that the DataGrid has brought to me. However, if we write a program-drawn table, we can flexibly control the form of the table, therefore, can a DataGrid customize its representation. The answer is: I know it is certainly possible. The key is whether it is faster than I use the code to "Draw.
Based on past experience, I know that these operations can be completed from the properties of the DataGrid.
1. Select the columns to display:
By default, the columns we display in the DataGrid are the columns We query when executing the SQL query statement. What does this mean? If Select * from myTable is used, all columns are displayed. If Select field1 and field2 from mytable are used, only two columns are displayed. Therefore, to select the columns to be displayed in the Datagrid, the simplest method is not to modify attributes, but to modify SQL query statements.
Another small problem is that when designing a database, the field name is often in English. However, we want to use Chinese in the DataGrid, in the select statement, we need to give each field a Chinese alias, such As: Select Name as Name, cardid As employee number from mytable.
Sometimes it is inconvenient for us to use this method to select the columns we want to display, so we can use the property generator to set them.
In the design view, select the DataGrid, and the "Property generator" link will appear at the bottom of the "Property Window". Click this link (or find the columns attribute) to bring up a dialog box, you can use this dialog box to set the columns to be selected:
First, we remove the hooks before "automatically create columns at runtime". In this way, the system will not automatically create columns, but will only display the columns you bind.
Click "column" on the left, and "Bind Column" and "button column" appear on the right. here we want to select "Bind Column ". Add a binding column and fill in items such as "header text. The header text refers to the page title. enter the name of the field to be displayed in "data field. Then, you need to bind several fields and select several binding columns.
2. Modify the DataGrid Style
By default, the table drawn from the DataGrid is ugly, so I need to customize its style. Select the DataGrid, and a "auto apply format" link will appear at the bottom of the Property Window. Click this link to bring up a dialog box for you to select a format and select your desired format.