DataGrid Web Control Depth Adventures (2) Part1

zhaozj2021-02-16  43

preface

In the first part we studied the basic functions of DataGrid, it can display data in the HTML table. In the first part, I explain that it is very simple to bind the database content to the DataGrid. What we have to do is to generate a DataReader object through the SQL query, set the DataGrid's DataSource property to this DataReader object, and then call the DataGrid object's DataBind ()method. The rest of the thing is to place the DataGrid into the HTML, which can be implemented by the following code:

It is so simple. Regrettably, the DataGrid generated through this simple method is not beautiful. The generated DataGrid is just a simple HTML table encapsulated in the DataReader.

We want to do only display only part of the DataReader and set the format of each column. And it is desirable to set the format that can be applied to the entire table such as a background color, font, and the like. Finally, if you can add a custom title for each column. For example, the background color of the set title is a different color or font as a bold. In this section we will study how to complete all these tasks! (DataGrid can do more things, we will see how to display database results in a series of articles, allowing users to sort data.)

Set DataGrid format

We have two ways to set formats for DataGrid. The first method is to set by the program in the server-side code. For example, in order to set the background color of the DataGrid to red, you can use the following server-side code:

<% @ Import namespace = "system.drawing"%>