GridView控件绑定到一个DataView对象

这篇博客展示了如何在ASP.NET中通过C#代码动态创建一个DataTable,并将其填充为DataView对象,然后将这个DataView绑定到GridView控件,以显示数据。示例代码包括了创建数据表、添加数据行以及数据绑定的过程。
摘要由CSDN通过智能技术生成
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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值