ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Filter Windows Form DataGridView with multiple ComboBox using C# and VB.Net<p>Hey <a class="username" rel="rockovo"> rockovo</a>,</p> <p>Please refer below sample.</p> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data.SqlClient; </pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data.SqlClient </pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">private void Form1_Load(object sender, EventArgs e) { &nbsp;&nbsp;&nbsp; this.PopulateComboBox(cbCountries, "SELECT DISTINCT Country FROM Employees", "Country", "Country"); &nbsp;&nbsp;&nbsp; this.PopulateComboBox(cbCities, "SELECT DISTINCT City FROM Employees", "City", "City"); &nbsp;&nbsp;&nbsp; this.PopulateDataGridView(); } private void PopulateComboBox(ComboBox comboBox, string query, string displayMember, string valueMember) { &nbsp;&nbsp;&nbsp; string constr = @"Data Source=.\SQL2005;Initial Catalog=Northwind;User ID=sa;Password=pass@123"; &nbsp;&nbsp;&nbsp; using (SqlConnection con = new SqlConnection(constr)) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; using (SqlCommand cmd = new SqlCommand(query, con)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; using (SqlDataAdapter sda = new SqlDataAdapter(cmd)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataTable dt = new DataTable(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sda.Fill(dt); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataRow row = dt.NewRow(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dt.Rows.InsertAt(row, 0); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; comboBox.DataSource = dt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; comboBox.DisplayMember = displayMember; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; comboBox.ValueMember = valueMember; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; } } private void PopulateDataGridView() { &nbsp;&nbsp;&nbsp; string query = "SELECT EmployeeId, Country, City FROM Employees"; &nbsp;&nbsp;&nbsp; query += " WHERE Country = @Country AND City = @City"; &nbsp;&nbsp;&nbsp; query += " OR ISNULL(@Country, '') = ''"; &nbsp;&nbsp;&nbsp; query += " OR ISNULL(@City, '') = ''"; &nbsp;&nbsp;&nbsp; string constr = @"Data Source=.\SQL2005;Initial Catalog=Northwind;User ID=sa;Password=pass@123"; &nbsp;&nbsp;&nbsp; using (SqlConnection con = new SqlConnection(constr)) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; using (SqlCommand cmd = new SqlCommand(query, con)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue("@Country", cbCountries.SelectedValue); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue("@City", cbCities.SelectedValue); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; using (SqlDataAdapter sda = new SqlDataAdapter(cmd)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataTable dt = new DataTable(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sda.Fill(dt); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dataGridView1.DataSource = dt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; } } private void Search(object sender, EventArgs e) { &nbsp;&nbsp;&nbsp; this.PopulateDataGridView(); }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load &nbsp;&nbsp;&nbsp; Me.PopulateComboBox(cbCountries, "SELECT DISTINCT Country FROM Employees", "Country", "Country") &nbsp;&nbsp;&nbsp; Me.PopulateComboBox(cbCities, "SELECT DISTINCT City FROM Employees", "City", "City") &nbsp;&nbsp;&nbsp; Me.PopulateDataGridView() End Sub Private Sub PopulateComboBox(ByVal comboBox As ComboBox, ByVal query As String, ByVal displayMember As String, ByVal valueMember As String) &nbsp;&nbsp;&nbsp; Dim constr As String = "Data Source=.\SQL2005;Initial Catalog=Northwind;User ID=sa;Password=pass@123" &nbsp;&nbsp;&nbsp; Using con As SqlConnection = New SqlConnection(constr) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Using cmd As SqlCommand = New SqlCommand(query, con) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Using sda As SqlDataAdapter = New SqlDataAdapter(cmd) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim dt As DataTable = New DataTable() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sda.Fill(dt) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim row As DataRow = dt.NewRow() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dt.Rows.InsertAt(row, 0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; comboBox.DataSource = dt &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; comboBox.DisplayMember = displayMember &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; comboBox.ValueMember = valueMember &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End Using &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End Using &nbsp;&nbsp;&nbsp; End Using End Sub Private Sub PopulateDataGridView() &nbsp;&nbsp;&nbsp; Dim query As String = "SELECT EmployeeId, Country, City FROM Employees" &nbsp;&nbsp;&nbsp; query += " WHERE Country = @Country AND City = @City" &nbsp;&nbsp;&nbsp; query += " OR ISNULL(@Country, '') = ''" &nbsp;&nbsp;&nbsp; query += " OR ISNULL(@City, '') = ''" &nbsp;&nbsp;&nbsp; Dim constr As String = "Data Source=.\SQL2005;Initial Catalog=Northwind;User ID=sa;Password=pass@123" &nbsp;&nbsp;&nbsp; Using con As SqlConnection = New SqlConnection(constr) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Using cmd As SqlCommand = New SqlCommand(query, con) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue("@Country", cbCountries.SelectedValue) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue("@City", cbCities.SelectedValue) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Using sda As SqlDataAdapter = New SqlDataAdapter(cmd) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim dt As DataTable = New DataTable() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sda.Fill(dt) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dataGridView1.DataSource = dt &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End Using &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End Using &nbsp;&nbsp;&nbsp; End Using End Sub Private Sub Search(ByVal sender As Object, ByVal e As EventArgs) &nbsp;&nbsp;&nbsp; Me.PopulateDataGridView() End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><span style="text-decoration: underline;"><strong><img src="https://i.imgur.com/ledjiCJ.gif" alt="" width="376" height="329" /></strong></span></p>https://www.aspforums.net:443/Threads/576020/Filter-Windows-Form-DataGridView-with-multiple-ComboBox-using-C-and-VBNet/https://www.aspforums.net:443/Threads/576020/Filter-Windows-Form-DataGridView-with-multiple-ComboBox-using-C-and-VBNet/Mon, 10 Dec 2018 00:25:42 GMT