Refer the below sample code for your reference and implement it as per your code logic.
Here i have used Northwind Database.
Form Design
C#
private void Form1_Load(object sender, EventArgs e)
{
PopulateComboBox();
GetRecordsBasedOnCountry();
}
private void CountryChanged(object sender, EventArgs e)
{
GetRecordsBasedOnCountry();
}
private void GetRecordsBasedOnCountry()
{
string query = "SELECT CustomerID, ContactName, Country FROM Customers";
query += " WHERE Country = @Country";
query += " OR ISNULL(@Country, '') = ''";
string constr = @"Data Source=.;Initial Catalog=Northwind;User ID=sa;Password=pass@123";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.Parameters.AddWithValue("@Country", cbCountry.SelectedIndex > 0 ? cbCountry.Text : "");
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
dgvDetails.DataSource = dt;
}
}
}
}
private void PopulateComboBox()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[1] { new DataColumn("Country",typeof(string))
});
dt.Rows.Add("Belgium");
dt.Rows.Add("Austria");
dt.Rows.Add("France");
dt.Rows.Add("Brazil");
dt.Rows.Add("Ireland");
dt.Rows.Add("Italy");
dt.Rows.Add("Finland");
dt.Rows.Add("Finland");
dt.Rows.Add("Argentina");
dt.Rows.Add("USA");
DataRow row = dt.NewRow();
row[0] = "Select Country";
dt.Rows.InsertAt(row, 0);
//Assign DataTable as DataSource.
cbCountry.DataSource = dt;
cbCountry.DisplayMember = "Country";
cbCountry.ValueMember = "Country";
}
VB.Net
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
PopulateComboBox()
GetRecordsBasedOnCountry()
End Sub
Private Sub CountryChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cbCountry.SelectedIndexChanged
GetRecordsBasedOnCountry()
End Sub
Private Sub GetRecordsBasedOnCountry()
Dim query As String = "SELECT CustomerID, ContactName, Country FROM Customers"
query += " WHERE Country = @Country"
query += " OR ISNULL(@Country, '') = ''"
Dim constr As String = "Data Source=.;Initial Catalog=Northwind;User ID=sa;Password=pass@123"
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(query, con)
cmd.Parameters.AddWithValue("@Country", If(cbCountry.SelectedIndex > 0, cbCountry.Text, ""))
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
dgvDetails.DataSource = dt
End Using
End Using
End Using
End Sub
Private Sub PopulateComboBox()
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(0) {New DataColumn("Country", GetType(String))})
dt.Rows.Add("Belgium")
dt.Rows.Add("Austria")
dt.Rows.Add("France")
dt.Rows.Add("Brazil")
dt.Rows.Add("Ireland")
dt.Rows.Add("Italy")
dt.Rows.Add("Finland")
dt.Rows.Add("Finland")
dt.Rows.Add("Argentina")
dt.Rows.Add("USA")
Dim row As DataRow = dt.NewRow()
row(0) = "Select Country"
dt.Rows.InsertAt(row, 0)
cbCountry.DataSource = dt
cbCountry.DisplayMember = "Country"
cbCountry.ValueMember = "Country"
End Sub
Screenshot