DataGrid Web 控件是三個Web 數據顯示控件之中功能最強的。我們在撰寫動態網頁的時候,常需要將數據以不同的風格呈現出來;DataGrid Web 控件和上述介紹的Repeater Web 控件及DataList Web 控件都可以辦到。但如果所要呈現的數據量非常龐大,而需要將這些數據分頁展示的話,那就要靠DataGrid Web 控件了;DataGrid Web 控件除了支持分頁的功能外,也可以讓使用者編修數據。其使用語法為:
<ASP:DataGrid
Id="被程序代碼所控制的名稱"
Runat="Server"
DataSource='<%#數據系結敘述%>'
AllowPaging="True | False"
AllowSorting="True | False"
AutoGenerateColumns="True | False"
BackImageUrl="url"
CellPadding="像素"
CellSpacing="像素"
DataKeyField="主鍵字段"
GridLines="None | Horizontal | Vertical | Both"
HorizontalAlign="Center | Justify | Left | NotSet | Right"
PagedDataSource
PageSize="ItemCount"
ShowFooter="True | False"
ShowHeader="True | False"
VirtualItemCount="ItemCount"
AlternatingItemStyle-Property="value"
EditItemStyle-Property="value"
FooterStyle-Property="value"
HeaderStyle-Property="value"
ItemStyle-Property="value"
PagerStyle-Property="value"
SelectedItemStyle-Property="value"
OnCancelCommand="事件程序"
OnDeleteCommand="事件程序"
OnEditCommand="事件程序"
OnItemCommand="事件程序"
OnItemCreated="事件程序"
OnPageIndexChanged="事件程序"
OnSortCommand="事件程序"
OnUpdateCommand="事件程序"
/>
或
<ASP:DataGrid
Id="被程序代碼所控制的名稱"
Runat="Server"
AutoGenerateColumns="False"
DataSource='<%# DataBindingExpression %>'
其它屬性設定...
<Property Name="Columns">
<ASP:BoundColumn/>
<ASP:EditCommandColumn/>
<ASP:HyperlinkColumn/>
<ASP:TemplateColumn>
樣版設定...
</ASP:TemplateColumn>
</Property>
</ASP:DataGrid>