When designing a website, we often need to display data in a database on a page. In the traditional ASP, you must create an HTML table that loops in a recordset (RECORDSET) to get each record and create a
First, drag and drop a DataGrid to a web form. Then write some data access code to get data from a database. Using the following code we can bind the data in the DataTable named MDTUsers to DGDUsers this DataGrid:
DGDUSERS.DataSource = MDTUSERS
DGDUSERS.DATABIND ()
figure 1.
Use a DataGrid to display the data
We set DataGrid's DataSource to DataTable, and then call the Database to bind the data. Such ASP.NET automatically creates a form for all data in DataTable. It arranges each column and displays the field name in the DataTable as a column header.
You can also modify the code to make DataGrid displays (see Figure 1) in the way you need (see Figure 1). For example, you can only display certain fields of DataTable on a web form, or you can use a different column header instead of the field name used by DataTable. .NET allows you to fully control the display of DataGrid.
Select the HTML Tab button at the bottom of the page to display the HTML code of the page. Find the
BackColor = "CORAL"> alternatingItemStyle> Add this row code to set the background color of the title: Now, suppose you don't want to display all the fields in the DataTable. First, you need to add an AutoGenerateColumns property to the Runat = "server" autogeneratecolumns = "false"> By default, AutoGenerateColumns is set to TRUE, which is telling .Net, let it generate all columns. If you want to control which columns are generated, set this property to false. Then, you need to add a Headertext = "Last Name"> asp: boundcolumn> Headeertext = "first name"> asp: boundcolumn> "Headertext =" email "> asp: boundcolumn> Columns> You use a Your DataGrid's complete HTML code should look like this: Runat = "server" autogeneratecolumns = "false"> BackColor = "CORAL"> alternatingItemStyle> BackColor = "crimson"> headerstyle> Headertext = "Last Name> asp: boundcolumn> Headertext = "first Name> asp: boundcolumn> Headertext = "email"> asp: boundcolumn> Columns> ask: DataGrid> ASP.NET simplifies many common website design work. By using a few lines of code you can display the data of the entire table, and it can also display data as you want.