The newly added GridView control in ASP.net 2.0 is indeed very powerful, making up for the drawbacks of using the DataGrid control in ASP.net 1.1. Because in ASP.net 1.1, when using a DataGrid, there are many cases where you still have to write a lot of code, which is inconvenient and sometimes requires a lot of skill. In ASP.net 2.0, in many cases, using the GridView control, you can even simply drag the control, set the properties, and do not need to write any code. In the GridView control using ASP.net 2.0 and the GridView control operation data in Asp.net2.0, a series of descriptions of the GridView control has been done, and if you have not previously heard of the GridView reader, read the two articles first. In this article, you will continue to delve into some of the usage techniques of the GridView.
One format GridView
Like ASP.net 1.1, the GridView can easily customize its style, such as CSS, colors, and so on. To customize the GridView format, it's easy to just right-click the GridView and select "AUTO FORMAT" from the pop-up menu to select the GridView style, with many styles in it, as shown in the following figure:
If you want to customize the format for each column in the GridView, just click on the "smart tag" smart tag in the top right-hand corner of the GridView, select "Edit Columns" in the pop-up menu, and the form below will pop up, so you can set the details for each column:
For example, if you want a column to be set to a special format, and if you want to set UnitPrice to currency format, you can set it to {0:C} in the DataFormatString property of the UnitPrice column, and the program code is as follows:
<%@ Page language= "C #"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd XHTML 1.1//en"
"HTTP://WWW.W3.ORG/TR/XHTML11/DTD/XHTML11.DTD" >
<script runat= "Server" >
</script>
<html xmlns= "http://www.w3.org/1999/xhtml" >
<head runat= "Server" >
<title>untitled page</title>
</head>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:sqldatasource id= "Productsdatasource"
runat= "Server"
selectcommand= "SELECT [ProductID], [ProductName],
[QuantityPerUnit], [UnitPrice], [UnitsInStock] from
[Products] "
connectionstring= "<%$ connectionstrings:nwconnectionstring%>"
datasourcemode= "DataReader" >
</asp:SqlDataSource>
<asp:gridview id= "Productgridview" runat= "Server"
datasourceid= "Productsdatasource"
datakeynames= "ProductID" autogeneratecolumns= "False"
borderwidth= "1px" backcolor= "#DEBA84"
cellpadding= "3" cellspacing= "2" borderstyle= "None"
bordercolor= "#DEBA84" >
<footerstyle forecolor= "#8C4510" backcolor= "#F7DFB5" ></FooterStyle>
<pagerstyle forecolor= "#8C4510" horizontalalign= "Center" ></PagerStyle>
<headerstyle forecolor= "white" font-bold= "True" backcolor= "#A55129" ></HeaderStyle>
<Columns>
<asp:boundfield readonly= "True" headertext= "ID" insertvisible= "False" datafield= "ProductID"
sortexpression= "ProductID" >
<itemstyle horizontalalign= "Center" > </ItemStyle>
</asp:BoundField>
<asp:boundfield headertext= "Name" datafield= "ProductName" sortexpression= "ProductName" >
</asp:BoundField>
<asp:boundfield headertext= "Qty/unit"
datafield= "QuantityPerUnit"
sortexpression= "QuantityPerUnit" > </asp:BoundField>
<asp:boundfield headertext= "Price/unit"
datafield= "UnitPrice" sortexpression= "UnitPrice"
dataformatstring= "{0:c}" >
<itemstyle horizontalalign= "right" > </ItemStyle>
</asp:BoundField>
<asp:boundfield headertext= "Units in the Stock" datafield= "UnitsInStock"
sortexpression= "UnitsInStock"
dataformatstring= "{0:d}" >
<itemstyle horizontalalign= "right" > </ItemStyle>
</asp:BoundField>
</Columns>
<selectedrowstyle forecolor= "White" font-bold= "True"
backcolor= "#738A9C" ></SelectedRowStyle>
<rowstyle forecolor= "#8C4510" backcolor= "#FFF7E7" ></RowStyle>
</asp:GridView>
</div>
</form>
</body>
</html>
After the program runs, the results are as follows:
In some cases, we may need to make a special display of the data in the GridView, such as when a certain stock of goods is 0 o'clock, requiring a different color display in the GridView, which can be done as follows:
First, the GridView provides the RowDataBound event, which is triggered after each row in the GridView is created and bound to the DataSource control, so we can use the event to check whether the inventory is 0, and if 0, set the Beijing color of the row to Yellow, the code is as follows:
public void productsGridView_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int unitsInStock = Convert.ToInt32(DataBinder.Eval (e.Row.DataItem, "UnitsInStock"));
if (unitsInStock == 0)
e.Row.BackColor = Color.Yellow;
}
}
First, the event checks to see whether the current row belongs to the DataRow type of row because, like the Headerrow,footerrow in the GridView, it does not contain the actual data, so we do not need to use HeaderRow and Footerrow, In order to get the contents of the inventory Unitesinstock, the content is removed by using the DataBinder.Eval method and converted to the int type, and then to determine if it is 0, and if 0, the background color of the row is yellow. The results of the program run as shown in the following illustration: