In the above example it is seen that when an item is not selected for editing, the EditCommandColumn displays an Edit command and on the selection of an item for editing, the EditCommandColumn displays Update and cancel commands.
An item can be selected for editing by clicking the Edit command displayed by an EditCommandColumn. When the Edit command link, is clicked the dgrdEmployeesEditCommand executes and assigns the index number of the selected item to the EdititemIndex property. This results in the item being selected for editing.
Using Styles with a DataGrid
The DataGrid control supports style objects that can be used to customize its appearance. Each style object is an instance of the TableItemStyle class, which has the following properties:
BorderStyle
The style used for borders around
BackColor
The background color displayed for an item
BorderWidth
The width of the border
BorderColor
The border color displayed for an item
CSS Class
The Cascading Style Sheet class associated with the item
Font
An instance of the FontInfo class, which indicates how the font should be formatted
ForeColor
The foreground color used for text
Height
The height of the item
HorizontalAlign
The horizontal alignment of the item (possible values are Center, Justify, Left, NotSet, and Right)
VerticalAlign
The vertical alignment of the item (possible values are Bottom, Middle, NotSet, and Top)
Width
The width of the item
Wrap
If True, the contents of an item are word-wrapped (the default value is True)
Any of these style properties can be used with the AlternatingItemStyle, EditItemStyle, FooterStyle, HeaderStyle, ItemStyle, and SelectedItemStyle styles of the DataGrid control. When a style property is set while declaring a control, a hyphen (-) can be used instead of the normal period (.) to indicate a property of an object.
The following example, displays the header with a bold, Arial font and a yellow background. Every item is displayed with a 9-point Arial font. Alternate items are displayed with an Alice Blue background
| Example 47 ExpertDataGridStyles.aspx s |
<%@ Import Namespace="System.Data.SqlClient" %>
<Script Runat="Server">
Sub Page_Load
If Not isPostBack Then
Dim conNorthwind As SqlConnection
Dim cmdSelect As SqlCommand
conNorthwind=New SqlConnection("Server=localhost;UID=sa;PWD =secret ; Database=Northwind" )
cmdSelect = New SqlCommand( "Select * From Products", conNorthwind )
conNorthwind.Open()
dgrdProducts.DataSource = cmdSelect.ExecuteReader()
dgrdProducts.DataBind()
conNorthwind.Close()
End If
End Sub
</Script>
<html>
<head><title>ExpertDataGridStyles.aspx</title></head>
<body>
<form Runat="Server">
<asp:DataGrid
ID="dgrdProducts"
CellPadding="8"
HeaderStyle-Font-Name="Arial"
HeaderStyle-Font-Bold="True"
HeaderStyle-BackColor="lightBlue"
ItemStyle-Font-Name="Arial"
ItemStyle-Font-Size="9pt"
AlternatingItemStyle-BackColor="lightGrey"
Runat="Server" />
</form>
</body>
</html>
|
| |
|