DataGrid Web Control Depth Adventure (2) Part2

Source: Internet
Author: User
Tags numeric visual studio
Datagrid|web| control in the first part of this article, we studied how to set the display properties of the DataGrid Web control and how to set the display of the DataGrid's headers, footers, rows, and alternating rows through a style. All of these techniques are either used to set the entire DataGrid display, or to set the display of rows in the DataGrid. But how do I set the display properties of the columns in the DataGrid? It's not difficult, but you'll know it when you read it.

Set which columns should be displayed

By default, the DataGrid generates a corresponding column for each column returned by the SQL query in the generated HTML table. In some cases, however, you want to display only a subset of the columns in the DataGrid. For example, in the example I am working on, the call to the Sp_popularity stored procedure shows the 10 most popular aspfaqs.com issues. It contains faqid columns, and perhaps I do not want to display the column.

If you do not want to display all the columns returned by the database query in the DataGrid, you must explicitly declare all the columns that you want to display. The first step is to set the AutoGenerateColumns property of the DataGrid to False. Once you've done this, you'll need to set the columns you want to display with the BoundColumn Web control, as follows:

<asp:datagrid runat= "Server" autogeneratecolumns= "False" >
<Columns>
<asp:boundcolumn datafield= "DatabaseColumnName1" .../>
<asp:boundcolumn datafield= "DatabaseColumnName2" .../>
...
<asp:boundcolumn datafield= "Databasecolumnnamen" .../>
</Columns>
</asp:datagrid>
For each column you want to display, you need to specify the columns that you want to display in the database by using a <asp:boundcolumn .../> tag that contains the DataField property. All of these boundcolumn tags must be included in the column tag. (You can also specify these binding columns programmatically, but it's not readable and requires a lot of code!) Note that only columns specified through the BoundColumn tag are displayed in the DataGrid, and you must specify the columns you want to display!

The advantage of the BoundColumn control is that it contains some formatting properties, including:

L headertext-the text to set the column headings.

L footertext-the text at the end of the column (remember to set the Showfooter to True if you want to display the footer in the DataGrid).

L headerstyle/footerstyle/itemstyle-contains the same properties as the DataGrid style. It is useful for setting columns to be centered, foreground color, background color, and so on.

l dataformatstring-formatting commands. (Refer to the following example; Refer to the documentation for 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 do not want to display faqid or Faqcategoryid columns, and we want to format numeric columns (viewcount) and date/Time columns (dateentered). In addition, we want the value of the numeric column to be centered. These can be done in a few lines that are easy to read and easy to understand:

<asp:datagrid runat= "Server" id= "Dgpopularfaqs"
Backcolor= "#eeeeee" width= "85%"
Horizontalalign= "Center"
Font-name= "Verdana" cellpadding= "4"
Font-size= "10pt" autogeneratecolumns= "False" >
Font-bold= "True" horizontalalign= "Center"/>
<alternatingitemstyle backcolor= "White"/>

<Columns>
<asp:boundcolumn datafield= "CatName" headertext= "Category Name"/>
<asp:boundcolumn datafield= "Description" headertext= "FAQ Description"/>
<asp:boundcolumn datafield= "Viewcount" dataformatstring= "{0:#,###}"
headertext= "views" itemstyle-horizontalalign= "Center"/>
<asp:boundcolumn datafield= "Submittedbyname" headertext= "Author"/>
<asp:boundcolumn datafield= "Submittedbyemail" headertext= "Author ' s Email"/>
<asp:boundcolumn datafield= "dateentered" headertext= "Date Added"
Dataformatstring= "{0:mm-dd-yyyy}"/>
</Columns>
</asp:datagrid>
The actual operating results are as follows:

Category Name
FAQ Description

Views
Date Added

Getting Started
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 cannot 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 example above, the code above specifies the specific column that needs to be displayed and has a specific format applied. Please note that dataformatestring looks very interesting. Its format is always {0:format string}. {0: ...} Specifies that the string is formatted by using the. specified) to format the first argument (the first parameter refers to the value of the particular column returned by DataReader). In the example I used the format string #,###, which preceded each 3 number with a comma; the format string mm-dd-yyyy specifies that the date/Time field is displayed in the month, day, and year format.

Conclusion

Take a moment to look at the first example (see the DataGrid Web Control Depth Adventure (1)) and the present example. The improvement is really big! Note that all of these styles and user interface improvements do not require a single line of code to be implemented. We just set some properties in the markup of the Web Control! In fact, if you are using a similar visual Studio. NET editor, you can set formatting options by clicking on some buttons, selecting some check boxes, and selecting some items in the list box. Imagine the lengthy code that needs to be written to achieve the same effect in a traditional ASP, and that will make you fall in love with asp.net if you don't have it now.



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.