Hi thewall813,
Refer below example.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Code
C#
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = this.GetData("SELECT TOP 5 CustomerId,ContactName,City,Country FROM Customers");
dataGridView1.AllowUserToAddRows = false;
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 1 && dataGridView1.Rows[e.RowIndex].Cells[1].Value != null)
{
string name = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
DataTable dt = this.GetData(string.Format("SELECT CustomerId,ContactName,City,Country FROM Customers WHERE ContactName = '{0}'", name));
dataGridView2.DataSource = dt;
dataGridView2.AllowUserToAddRows = false;
var colCity = this.dataGridView2.Columns[2];
this.dataGridView2.Columns.Remove(colCity);
var cmbColumn = new DataGridViewComboBoxColumn();
cmbColumn.DataSource = this.GetData("SELECT DISTINCT City FROM Customers");
cmbColumn.DataPropertyName = "City";
cmbColumn.ValueMember = "City";
cmbColumn.DisplayMember = "City";
cmbColumn.HeaderText = "City";
cmbColumn.ValueType = typeof(string);
cmbColumn.DefaultCellStyle.NullValue = "";
this.dataGridView2.Columns.Insert(2, cmbColumn);
}
}
private DataTable GetData(string sql)
{
using (SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Northwind;uid=sa;pwd=pass@123;"))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
}
}
VB.Net
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
dataGridView1.DataSource = Me.GetData("SELECT TOP 5 CustomerId,ContactName,City,Country FROM Customers")
dataGridView1.AllowUserToAddRows = False
End Sub
Private Sub dataGridView1_CellClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles dataGridView1.CellClick
If e.ColumnIndex = 1 AndAlso dataGridView1.Rows(e.RowIndex).Cells(1).Value IsNot Nothing Then
Dim name As String = dataGridView1.Rows(e.RowIndex).Cells(1).Value.ToString()
Dim dt As DataTable = Me.GetData(String.Format("SELECT CustomerId,ContactName,City,Country FROM Customers WHERE ContactName = '{0}'", name))
dataGridView2.DataSource = dt
dataGridView2.AllowUserToAddRows = False
Dim colCity = Me.dataGridView2.Columns(2)
Me.dataGridView2.Columns.Remove(colCity)
Dim cmbColumn = New DataGridViewComboBoxColumn()
cmbColumn.DataSource = Me.GetData("SELECT DISTINCT City FROM Customers")
cmbColumn.DataPropertyName = "City"
cmbColumn.ValueMember = "City"
cmbColumn.DisplayMember = "City"
cmbColumn.HeaderText = "City"
cmbColumn.ValueType = GetType(String)
cmbColumn.DefaultCellStyle.NullValue = ""
Me.dataGridView2.Columns.Insert(2, cmbColumn)
End If
End Sub
Private Function GetData(ByVal sql As String) As DataTable
Using con As SqlConnection = New SqlConnection("Data Source=.;Initial Catalog=Northwind;uid=sa;pwd=pass@123;")
Using cmd As SqlCommand = New SqlCommand(sql, con)
cmd.CommandType = CommandType.Text
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Using
End Function
End Class
Screenshot