In the first part of this article, we have studied how to set the display attribute of the DataGrid Web control and how to set the data of the DataGrid through the style. All of these techniques or to set the display of the entire DataGrid, or to set the display of the line of the DataGrid. But how do I set the display attributes listed in DataGrid? It's not difficult, then I will know it.
Settings which columns should be displayed
By default, DataGrid generates a corresponding column for each column returned for the SQL query in the generated HTML table. But in some cases, you only want to display a part of these columns in the DataGrid. For example, in the example I am in progress, the most popular 10 questions in Aspfaqs.com are displayed by calling the sp_popularity stored procedure. It contains the FAQID column, maybe I don't want to display the column.
If you do not want to display all columns returned by the database query in the DataGrid, you must explicitly declare all the columns you want to display. The first step is to set the DataGrid's AutoGenerateColumns property to false. Once this is performed, you need to set the columns you want to display through the BoundColumn web control, as shown below:
...
Columns>
ask: DataGrid>
For each column that you want to display, you need to specify the columns you need to display in the database through a
The advantage of the BoundColumn control is that it contains some properties of the setting format, including:
l Headeertext - Set the text of the column header.
l Footertext - Set the text of the column (Remember To display the footer in the DataGrid, you should set ShowFooter to true).
l Headerstyle / Footerstyle / ItemStyle - Contains the same properties as the DataGrid style. It is useful for setting up, foreground, background color, etc.
l DataFormatString - Set the format command. (Refer to the example below; refer to the documentation to get all formatting specifications)
Let's take a look at how to further enhance the previous example by using the BoundColumn tag. As mentioned earlier, we don't want to display the FAQID or FAQCATEGORYID column, and we want to set the format for the digital column (DateEntered). In addition, we hope that the value of the numeric column is in. These can be completed by several lines of easy-to-understand code:
Backcolor = "# eeeeee" width = "85%" horizontalalign = "center" Font-name = "verdana" Cellpadding = "4" Font-size = " 10PT "AutogenerateColumn =" false "> Font-bold = "true" horizontalalign = "center" /> Headertext = "views" itemstyle-horizontalalign = "center" /> DataFormatString = "{0: MM-DD-YYYY}" /> Columns> ask: DataGrid> The actual operation is as follows: Category Name FAQ DESCRIPTION Views Date Added Getting start WHERE CAN I HOST MY ASP Web Site for Free (SIMILAR TO Geocities or tripod or any of the many Other Free Web Site Sites)? ... 161,316 03-20-2001 ASP.NET How can I Format NumBers and Date / Times Using ASP.NET? For Example, I Want To Format A Number As a Currency. 124,391 01-19-2002 Databases, Errors I am using Access and getting a 80004005 error (or a [Microsoft] [ODBC Microsoft Access Driver] The Microsoft Jet database engine can not open the file '(unknown)' error) when trying to open a connection! How can I fix this problem ? 108,374 01-17-2001 ... As shown in the above example, the above code specifies a particular column that needs to be displayed and a specific format is applied. Note that DataFormateString looks very interesting. Its format is always {0: Format string}. {0: ...} Specifies format the first parameter by formatting strings (specified by ...) (the first parameter refers to the value of the particular column returned by DataReader). In the example, I used the formatted string #, ###, which plus a comma before every 3 numbers; formatted string mm-dd-yyyy specifies the date / time of the month, day and year Field. in conclusion Take some time to look at the first example (see DataGrid Web Control Depth Adventures (1)) and the present example. Improvement is really large! Please pay attention to all these style and user interface improvements can be implemented. We just set some properties in the tag of the web control! In fact, if you are using an editor similar to Visual Studio .NET, you can select some check box by clicking some buttons, select some of the items to set the formatting option. Imagine those lengthy code that needs to be written in traditional ASP, which will make you fall in love with asp.net if you haven't yet. I wish you a happy program!