using System;
using System.Data;
using System.Web.UI.WebControls;
public partial class GridViewExample : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// 创建一个DataTable来模拟数据源
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Age", typeof(int));
// 添加一些示例数据
dt.Rows.Add(1, "Alice", 30);
dt.Rows.Add(2, "Bob", 25);
dt.Rows.Add(3, "Charlie", 35);
// 将DataTable绑定到GridView控件
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
if (!IsPostBack)
确保代码只在首次加载页面时执行,而不是在每次回发时。DataTable
来模拟数据源。这里我们添加了三列:ID
、Name
和 Age
。DataTable
中添加了几行示例数据。DataTable
绑定到 GridView
控件,并调用 DataBind()
方法以显示数据。GridView1
是页面上定义的 GridView
控件的 ID。确保在 .aspx
文件中有相应的控件定义。上一篇:c# int转byte
下一篇:c# 入门
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站