Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c# gridview

作者:吃斋的狼   发布日期:2025-09-03   浏览:104

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();
        }
    }
}

解释说明:

  1. Page_Load 方法:在页面加载时执行。if (!IsPostBack) 确保代码只在首次加载页面时执行,而不是在每次回发时。
  2. 创建 DataTable:使用 DataTable 来模拟数据源。这里我们添加了三列:IDNameAge
  3. 添加示例数据:向 DataTable 中添加了几行示例数据。
  4. 绑定数据到 GridView:将 DataTable 绑定到 GridView 控件,并调用 DataBind() 方法以显示数据。

注意:

  • 该代码适用于 ASP.NET Web Forms 项目。
  • GridView1 是页面上定义的 GridView 控件的 ID。确保在 .aspx 文件中有相应的控件定义。

上一篇:c# int转byte

下一篇:c# 入门

大家都在看

c# 二进制

c# tcp client

c# type.gettype

c# sqlconnection

c# string.format 小数位数

.net和c#

c#获取系统时间

c#游戏开发

c#网络编程

c# rectangle

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站