I have added the Comboxbox outside of DataGridView. At Selected Index change event of Combobox i am Populating the DataGridView.
Please refer this code.
C#
Namespaces
using System.Data.SqlClient;
using System.Configuration;
Code
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.PopulateCombobox();
this.dataGridView1.AllowUserToAddRows = false;
}
private void PopulateCombobox()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT DISTINCT Country FROM Customers", conn))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
// Create a new row
DataRow dr = dt.NewRow();
dr["Country"] = "";
dt.Rows.InsertAt(dr, 0);
this.comboBox1.DisplayMember = "Country";
this.comboBox1.ValueMember = "Country";
this.comboBox1.DataSource = dt;
}
}
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT ContactName ,City FROM Customers WHERE Country = @Country OR @Country = ''", conn))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
cmd.Parameters.AddWithValue("@Country", this.comboBox1.SelectedValue);
DataTable dt = new DataTable();
da.Fill(dt);
this.dataGridView1.DataSource = dt;
}
}
}
}
}
VB
Namespaces
Imports System.Data.SqlClient
Imports System.Configuration
Code
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.PopulateCombobox()
Me.DataGridView1.AllowUserToAddRows = False
End Sub
Private Sub PopulateCombobox()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using conn As New SqlConnection(constr)
Using cmd As New SqlCommand("SELECT DISTINCT Country FROM Customers", conn)
Using da As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
da.Fill(dt)
' Create a new row
Dim dr As DataRow = dt.NewRow()
dr("Country") = ""
dt.Rows.InsertAt(dr, 0)
Me.comboBox1.DisplayMember = "Country"
Me.comboBox1.ValueMember = "Country"
Me.comboBox1.DataSource = dt
End Using
End Using
End Using
End Sub
Private Sub comboBox1_SelectedIndexChanged_1(sender As System.Object, e As System.EventArgs) Handles comboBox1.SelectedIndexChanged
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using conn As New SqlConnection(constr)
Using cmd As New SqlCommand("SELECT ContactName ,City FROM Customers WHERE Country = @Country OR @Country = ''", conn)
Using da As New SqlDataAdapter(cmd)
cmd.Parameters.AddWithValue("@Country", Me.comboBox1.SelectedValue)
Dim dt As New DataTable()
da.Fill(dt)
Me.DataGridView1.DataSource = dt
End Using
End Using
End Using
End Sub
End Class
Database
I am using Northwind database and for more details refer the ASPSnippets article
Install Microsoft Northwind and Pubs Sample databases in SQL Server Management Studio
Screenshot
