Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a Gridview and a Dropdown list in my aspx page. I want to set/fill the Gridview data When i select something from the dropdown list(dropdownlist1.SelectIndexChanged event), how to do it?
i want to do it in code behind file(cs file).
Thank you in advance
Posted
Comments
member 8888995 16-Dec-13 7:04am    
Hi,
Do Something like this
1.Read dropdownlist1's selected item in a variable.
2.Then use this value in your query in where clause.
3.Execute this query and get data in a dataset.
4.Set this dataset as datasource to grid.

--
Thanks
SumitChandra 19-Dec-13 0:04am    
Thanks for your suggesion.... It works fine

C#
inside the dropdownlist.selectedchange event
{
       string str="select * from table name where Id="+dropdownlist.seletedvalue+";
       SqlCommand cmd = new SqlCommand(str, con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        
       gridview.datasource=ds.tables[0];
       gridview.databind();
}
 
Share this answer
 
v2
Comments
SumitChandra 19-Dec-13 0:06am    
Thanks for your comment..
Pass the Dropdownlist selected value or text to your sql query. Then the bind the resulted dataset to your gridview. and then bind the gridview.
 
Share this answer
 
Comments
SumitChandra 19-Dec-13 0:06am    
Thanks for your comment... i found it..how to do it via dataset...Thanks a lot
[no name] 19-Dec-13 0:20am    
As mentioned below by.. ErBhati


inside the dropdownlist.selectedchange event
{
string str="select * from table name where Id="+dropdownlist.seletedvalue+";
SqlCommand cmd = new SqlCommand(str, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);

gridview.datasource=ds.tables[0];
gridview.databind();
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900