-2

I have a drop down list ; a sqldatasource and a gridview . I want to fill the grid view by the choice in the drop down list.

How can i implement this fill ?

Thanks

My question is not a duplicate of this Click

because it want to populate a drop down list inside the grid view instead my question is : by a choice of a drop down list , fill a grid view.

EDIT (Because i am in negative rep.) :

What i have tried

    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;
          }     
       }   
    }
}
Emanuel Pirovano
  • 226
  • 1
  • 6
  • 20
  • could you please use: [mcve] or at least show what you tried? – Mederic Jun 12 '17 at 09:13
  • Yes but i don't understand how can i link a drop down list with a grid view, so i haven't tried – Emanuel Pirovano Jun 12 '17 at 09:15
  • did you try this? https://www.google.fr/search?q=asp.net+link+dropdownlist+with+grid&rlz=1C1NHXL_enFR738FR738&oq=asp.net+link+dropdownlist+with+grid&aqs=chrome..69i57j0l2.2521j0j4&sourceid=chrome&ie=UTF-8 – Mederic Jun 12 '17 at 09:17
  • Possible duplicate of [how to bind a dropdownlist in gridview?](https://stackoverflow.com/questions/7329224/how-to-bind-a-dropdownlist-in-gridview) – Mederic Jun 12 '17 at 09:18
  • Yeah, google is my best friend, watch the result : dropdown list inside grid view and other . Anyway thanks a lots – Emanuel Pirovano Jun 12 '17 at 09:19
  • yeah exactly before asking a question should try to use the MSDN to see if there is a DataBind method and should try google and also should try the search on stack overflow as duplicate answers with no effort will simply attract downvote – Mederic Jun 12 '17 at 09:21
  • I know, in your opinion , refer to this https://stackoverflow.com/questions/7329224/how-to-bind-a-dropdownlist-in-gridview . Is my question duplicate ? I think that are some difference – Emanuel Pirovano Jun 12 '17 at 09:29
  • Well you didn't explain clearly what you wanted and didn't show any effort of trying some code so it makes it hard to understand hence the Flag for: **Possible Duplicate** – Mederic Jun 12 '17 at 09:31

1 Answers1

1

aspx page
<asp:DropDownList runat="server" ID="drp" AutoPostBack="true" OnSelectedIndexChanged="drp_SelectedIndexChanged"></asp:DropDownList> <asp:GridView runat="server" ID="grd" AutoGenerateColumns="true"></asp:GridView>

code behind of aspx

protected void drp_SelectedIndexChanged(object sender, EventArgs e)
 {
        BindGrid();
 }

 private void BindGrid()
 {
        grd = dataSource;
        grd.DataBind();
 }

Bind grid on selection index change event of dropdown.

Yogesh Patel
  • 818
  • 2
  • 12
  • 26