How to Bind a GridView from database

前端 未结 6 2062
梦谈多话
梦谈多话 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:25

    Try below code according to your scenario

    I hope it helps you

    protected void GridviewBind ()
    {
        using (SqlConnection con = new SqlConnection("Data Source=RapidProgramming;Integrated Security=true;Initial Catalog=RPDB"))
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("Select Name,Salary FROM YOUR TABLE", con);
            SqlDataReader dr = cmd.ExecuteReader();
            GridView1.DataSource = dr;
            GridView1.DataBind();
            con.Close();
        }
    }
    
    
      
      
      
      
      
      
      
      
      
    ;
    

提交回复
热议问题