using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class CH10_DemoForm002 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt;
DataRow dr;
int i;
// 创建一个 DataTable 对象(也就是数据表)。
dt = new DataTable();
// 替数据表加入字段。
dt.Columns.Add(new DataColumn("编号", typeof(int)));
dt.Columns.Add(new DataColumn("姓名", typeof(string)));
dt.Columns.Add(new DataColumn("出生日期", typeof(DateTime)));
dt.Columns.Add(new DataColumn("性别", typeof(bool)));
dt.Columns.Add(new DataColumn("薪资", typeof(double)));
// 新建 3 笔数据行至数据表。
dr = dt.NewRow();
dr[0] = "0001";
dr[1] = "章立民";
dr[2] = "1988/01/01";
dr[3] = true;
dr[4] = 50000;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "0002";
dr[1] = "洪志豪";
dr[2] = "1968/01/01";
dr[3] = true;
dr[4] = 60000;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "0003";
dr[1] = "陈小惠";
dr[2] = "1981/01/01";
dr[3] = false;
dr[4] = 30000;
dt.Rows.Add(dr);
// 将 GridView 控件绑定到 DataView 对象。
GridView1.DataSource = new DataView(dt);
GridView1.DataBind();
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CH10_DemoForm002.aspx.cs" Inherits="CH10_DemoForm002" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>示范以编程方式将 GridView 控件绑定到一个 DataView 对象</title>
<style type="text/css">
#form1
{
text-align: center;
}
body
{
font-family: Lucida Sans Unicode;
font-size: 10pt;
}
button
{
font-family: tahoma;
font-size: 8pt;
}
.highlight
{
display: block;
color: red;
font: bold 24px Arial;
margin: 10px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>