LINQ绑定List到GridView

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace _126
{
    public partial class _126 : System.Web.UI.Page
    {
        Data126DataContext data = new Data126DataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var users = from p in data._126
                            select new
                            {
                                p.id,
                                p.num,
                                p.time
                            };

                gve.DataSource = users.ToList();
                gve.DataBind();
            }
            
        }

      
        protected void Button1_Click(object sender, EventArgs e)
        {
            _126 num = new _126();
            num.num = txtNum.Text;
            num.time = DateTime.Now;

            data._126.InsertOnSubmit(num);
            data.SubmitChanges();

            
        }
    }
}

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="126.aspx.cs" Inherits="_126._126" %>

<!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 runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        数据:<asp:TextBox ID="txtNum" runat="server"></asp:TextBox>
&nbsp;<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="添加" />
        <br />
        <asp:GridView ID="gve" runat="server" AutoGenerateColumns="False">
            <Columns>
              <asp:BoundField DataField="id" HeaderText="用户名"/>
              <asp:BoundField DataField="num" HeaderText="密码"/>
              <asp:BoundField DataField="time" HeaderText="用户信息"/>
              </Columns>
        </asp:GridView>
    
    </div>
    </form>
</body>
</html>

 

你可能感兴趣的:(GridView)