You need to pass the selected item from the
DropDownList
in your sql query or SP if you want to filter
GridView
based on the
DropDownList
selection. Here's a quick example for your reference:
<br />
protected void BindGrid(string selectedItem){<br />
using(SqlConnection connection = new SqlConnection("YOUR CONNECTION STRING HERE")){<br />
string sql = "SELECT Field1, Field2, Field3 FROM YourTableName WHERE YourFieldName = @Param1";<br />
using(SqlCommand cmd = new SqlCommand(sql,connection)){<br />
cmd.Parameters.AddWithValue("@Param1", selectedItem);<br />
<br />
DataTable dt = new DataTable();<br />
SqlDataAdapter ad = new SqlDataAdapter(cmd);<br />
ad.Fill(dt);<br />
<br />
if (dt.Rows.Count > 0) {
GridView1.DataSource = dt;<br />
GridView1.DataBind();<br />
}<br />
else<br />
{<br />
}<br />
}<br />
<br />
}<br />
}<br />
Then at
SelectedIndexChanged
event of your dropdown, you can call the method above like this:
<br />
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e){<br />
BindGrid(DropDownList1.SelectedItem.Text);<br />
}<br />
Just make sure to set
AutoPostback
to true for your dropdown to fire the
selectedindexchanged
event.