How to Bind a GridView from database

前端 未结 6 2066
梦谈多话
梦谈多话 2020-12-11 19:24

How to bind a GridView?

I want to display my table data in a gridview.

I have createed SQL table EmpDetail with columns ID, Name, Salary D

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-11 20:22

    
    
    
    protected void Page_Load(object sender, EventArgs e) {
        if (!IsPostBack) {
            bindData();   
        }
    }
    
    public void bindData() {
            SqlConnection con=new SqlCponnection(ConnectionStrings);
            SqlDataAdapter da = new SqlDataAdapter("select * from  Your TableName", con);
            DataSet ds = new DataSet();
            try {
                da.Fill(ds, "YourTableName");
                GridView1.DataSource = ds;
                GridView1.DataBind();
            } catch (Exception e) {
               Response.Write( e.Message);
            } finally {
                ds.Dispose();
                da.Dispose();
                con.Dispose();
            }
    

提交回复
热议问题