how to bind a dropdownlist in gridview?

后端 未结 5 1895
攒了一身酷
攒了一身酷 2020-12-03 03:52

I have a gridview in which every row contains a dropdownlist. I want to bind every dropdownlist dynamically. Can someone tell me how can i do it. Thanks in Advance

5条回答
  •  时光取名叫无心
    2020-12-03 04:41

    Binding the GridView

    Below is the code to Bind the GridView control with data.

    C#

    protected void Page_Load(object sender, EventArgs e)
    {
       if (!IsPostBack) 
       {
         this.BindData();   
       }
    } 
    
    private void BindData()
    {
       string query = "SELECT top 10 * FROM Customers";    
       SqlCommand cmd = new SqlCommand(query);    
       gvCustomers.DataSource = GetData(cmd);    
       gvCustomers.DataBind(); 
    }
    
    private DataTable GetData(SqlCommand cmd)
    {    
        string strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;    
        using (SqlConnection con = new SqlConnection(strConnString))   
        {        
           using (SqlDataAdapter sda = new SqlDataAdapter())     
           {            
              cmd.Connection = con;            
              sda.SelectCommand = cmd;            
              using (DataTable dt = new DataTable())  
              {                
                  sda.Fill(dt);                
                  return dt;
              }     
           }   
        }
    }
    

提交回复
热议问题