当然可以。ASp.NET GridView 控件可以绑定到 List<T> 类型的数据源。以下是一个简单的示例,说明如何将 GridView 控件绑定到 List<T> 类型的数据源:
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public string Position { get; set; }
}
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Id" HeaderText="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Position" HeaderText="Position" />
</Columns>
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
}
private void BindGridView()
{
List<Employee> employees = new List<Employee>
{
new Employee { Id = 1, Name = "John Doe", Position = "Developer" },
new Employee { Id = 2, Name = "Jane Doe", Position = "Designer" },
new Employee { Id = 3, Name = "Mike Smith", Position = "Manager" }
};
GridView1.DataSource = employees;
GridView1.DataBind();
}
这样,你就可以将 GridView 控件绑定到 List<T> 类型的数据源,并在页面上显示数据。
领取专属 10元无门槛券
手把手带您无忧上云