Feel the convenience that the DataGrid brings to data operations (2)

Source: Internet
Author: User
Tags bind header
datagrid| data
Section II: Customizing the appearance of the DataGrid

In the previous article, I initially felt the convenience of the DataGrid, but if we had written our own program, we would be able to control the form, so that the DataGrid could customize its presentation. The answer, I know, is definitely OK, and the key is whether it's faster than my own code "draw".

Based on past experience, I know that these operations can be done 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 SQL query statements, what does that mean? If we use the SELECT * from MyTable, then all columns will be displayed, and if we use the Select Field1,field2 from myTable then only two columns are displayed. So, the easiest way to select the columns you want to display in the DataGrid is not to modify the attributes, but to do so by modifying the SQL query statement.

Another small problem is that when we design a database, the name of the field is often in English, but we want to use Chinese in the DataGrid, we need to give each field a Chinese alias in the SELECT statement, for example: select name as name, Cardid As work No. from MyTable.

Sometimes it's not convenient for us to choose the columns we want to display in this way, so we use the Property builder to set it up.

In Design view, the DataGrid is selected, then the Property window has a link to the property builder at the bottom, and clicking the link (or finding the Columns property) pops up a dialog box that lets you set the columns to select:

First, we remove the "Automatically create columns at runtime" hook, so that the system no longer automatically creates columns and displays only the columns you bind.

Click Columns on the left, "Bound columns" and "button columns" on the right, and so on, here we select bind columns. Add a bound column, and then fill in the items such as header text. Header text refers to the page title, "Data field" fill in the name of the field you want to display. Then, to bind a few fields, select a few bound columns.



2, modify the style of the DataGrid

By default the DataGrid draws a table that is difficult to read, so I need to customize its style. With the DataGrid selected, an AutoFormat link appears at the bottom of the Properties window, and clicking the link pops up a dialog box that lets you choose the format you prefer.

I don't know what you're going to think, anyway, I think there are two formats that fit my appetite, and they fit the company's design specifications, so I tend to use these formats. If you're not satisfied, then you need to customize it yourself. Customization is also very simple, that is, you have to modify the "style" Class attribute value is OK. In addition, you can also use the Property Builder to complete the modification of the style properties. For example, if you don't want your form to go back, then you go to the Property Builder, select Format, and then, on the right, select "Item" To remove the hook in the front of the "cell text wrapping" in each sub-item to make it OK.





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.