qq_42450386
吃瓜日常
2018-12-21 08:43
采纳率: 33.3%
浏览 1.4k

ASP.NET WebForm中如何后台给GridView控件绑定数据?

前台代码:

<body>
    <form id="form1" runat="server">
    <div>
        &nbsp;<asp:GridView 
            ID="GridView1" runat="server" 
            onselectedindexchanged="GridView1_SelectedIndexChanged">
        </asp:GridView>
    </div>
    </form>
</body>

后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Chart
{
    public partial class Chart3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • IT_0802
    IT_0802 2018-12-21 01:07
    已采纳

    string sql = "server=.;database=你的数据库;integrated security=sspi;";
    string s = "select 字段,字段 from 表";
    SqlConnection conn = new SqlConnection(sql);
    SqlDataAdapter sda = new SqlDataAdapter(s,conn);
    DataSet ds = new DataSet();
    sda.Fill(ds);
    GridView1.DataSource = ds;
    GridView1.DataBind();

    点赞 评论
  • gl_net
    gl_net 2018-12-26 01:42

    在你想要绑定数据的地方放上就行,ds是dataset类型
    GridView1.DataSource = ds;
    GridView1.DataBind();

    点赞 评论

相关推荐