Here I have created sample that will help you out.
HTML
<div>
Country:
<asp:DropDownList ID="ddlCountry" runat="server">
<asp:ListItem Text="All" Value="" />
<asp:ListItem Text="Argentina" Value="Argentina" />
<asp:ListItem Text="Belgium" Value="Belgium" />
<asp:ListItem Text="Brazil" Value="Brazil" />
<asp:ListItem Text="Canada" Value="Canada" />
</asp:DropDownList>
<asp:Button Text="Search" runat="server" OnClick="Search" />
<br />
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderStyle-Width="150px" DataField="ContactName" HeaderText="Contact Name"
ItemStyle-CssClass="ContactName" />
<asp:BoundField HeaderStyle-Width="150px" DataField="CustomerID" HeaderText="CustomerID" />
<asp:BoundField HeaderStyle-Width="150px" DataField="City" HeaderText="City" />
<asp:BoundField HeaderStyle-Width="150px" DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
</div>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
BindGrid();
}
}
protected void Search(object sender, EventArgs e)
{
BindGrid();
}
private void BindGrid()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT top 10 * FROM Customers where Country=@Country or @Country=''";
SqlCommand cmd = new SqlCommand(query);
cmd.Parameters.AddWithValue("@Country", ddlCountry.SelectedItem.Value);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
gvCustomers.DataSource = ds;
gvCustomers.DataBind();
}
}
}
}
VB
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Not Me.IsPostBack Then
BindGrid()
End If
End Sub
Protected Sub Search(sender As Object, e As EventArgs)
BindGrid()
End Sub
Private Sub BindGrid()
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "SELECT top 10 * FROM Customers where Country=@Country or @Country=''"
Dim cmd As New SqlCommand(query)
cmd.Parameters.AddWithValue("@Country", ddlCountry.SelectedItem.Value)
Using con As New SqlConnection(conString)
Using sda As New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using ds As New DataSet()
sda.Fill(ds)
gvCustomers.DataSource = ds
gvCustomers.DataBind()
End Using
End Using
End Using
End Sub
Screenshot
