位置:海鸟网 > IT > ASP.NET >

怎样使用DataGrid控件

[Visual Basic, C#, JScript] 下面的示例展示如何使用 DataGrid 控件来显示数据源中的项。
[Visual Basic]
<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>

<html>
<script language="VB" runat="server">
Function CreateDataSource() As ICollection
Dim dt As New DataTable()
Dim dr As DataRow

dt.Columns.Add(New DataColumn("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn("CurrencyValue", GetType(Double)))

Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()

dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 *(i + 1)

dt.Rows.Add(dr)
Next i

Dim dv As New DataView(dt)
Return dv
End Function 'CreateDataSource


Sub Page_Load(sender As Object, e As EventArgs)

If Not IsPostBack Then
' Load this data only once.
ItemsGrid.DataSource = CreateDataSource()
ItemsGrid.DataBind()
End If
End Sub 'Page_Load

</script>

<body>

<form runat=server>

<h3>DataGrid Example</h3>

<b>Product List</b>

<asp:DataGrid
BorderColor="black"
BorderWidth="1"
CellPadding="3"
AutoGenerateColumns="true"
runat="server">

<HeaderStyle BackColor="#00aaaa">
</HeaderStyle>

</asp:DataGrid>

</form>

</body>
</html>