In this article I will explain with an example, how to set cell value of ComboBox in DataGridView programmatically from Database in Windows Forms (WinForms) Application using C# and VB.Net.
ComboBox will be added using the DataGridViewComboBoxColumn class and the items will be added to the ComboBox from Database.
Once the ComboBox is populated, the selected item will be set in ComboBox in Windows Forms (WinForms) Application using C# and VB.Net.
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
Note: You can download the database table SQL by clicking the download link below.
The Form Design consists of a DataGridView control.
You will need to import the following namespaces.
Set cell value of ComboBox in DataGridView programmatically from Database
Populating the DataGridView
Inside the Form Load event, the DataGridView is populated from Database using the GetData method which populates records from the Customers table into a DataTable.
Adding the ComboBox to DataGridView
The ComboBox is added by creating a DataGridViewComboBoxColumn and adding it to the DataGridView control.
Adding default item to the ComboBox
Then a loop is executed over the DataGridView Rows and the Cell containing the ComboBox is referenced and a default item such as “Please Select” is added to the ComboBox and also set as selected.
Populating the ComboBox
Finally, a DataTable is populated with list of Countries using the GetData method and using a FOR loop one by one items are added to the ComboBox and the Country value associated with the Customer record is set as selected.
Note: For illustration purpose, the Country value from Database is not set as selected so that it displays the default selected value.
private const string ConnectionString = @"Data Source=.\SQL2017;Initial Catalog=AjaxSamples;Integrated Security = true";
private void Form1_Load(object sender, EventArgs e)
//Fetch the data from Database.
dataGridView1.DataSource = this.GetData("SELECT CustomerId, Name FROM Customers");
dataGridView1.AllowUserToAddRows = false;
//Add a ComboBox Column to the DataGridView.
DataGridViewComboBoxColumn comboBoxColumn = new DataGridViewComboBoxColumn();
comboBoxColumn.HeaderText = "Country";
comboBoxColumn.Width = 100;
comboBoxColumn.Name = "comboBoxColumn";
//Loop through the DataGridView Rows.
foreach (DataGridViewRow row in dataGridView1.Rows)
//Reference the ComboBoxCell.
DataGridViewComboBoxCell comboBoxCell = (row.Cells as DataGridViewComboBoxCell);
//Insert the Default Item to ComboBoxCell.
//Set the Default Value as the Selected Value.
comboBoxCell.Value = "Select Country";
//Fetch the Countries from Database.
DataTable dt = this.GetData("SELECT CustomerId, Country from Customers");
//Loop through the DataTable Rows.
foreach (DataRow drow in dt.Rows)
//Fetch the CustomerId (Primary Key) value.
string customerId = drow.ToString();
//Add the Country value to the ComboBoxCell.
//Except for CustomerId #3.
if (customerId != "3")
//Compare the value of CustomerId.
if (row.Cells.Value.ToString() == customerId)
//Once CustomerId is matched, select the Country in ComboBoxCell.
comboBoxCell.Value = drow;
private DataTable GetData(string sql)
using (SqlConnection con = new SqlConnection(ConnectionString))
using (SqlCommand cmd = new SqlCommand(sql, con))
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
using (DataTable dt = new DataTable())
Dim ConnectionString As String = "Data Source=.\SQL2017;Initial Catalog=AjaxSamples;Integrated Security = true"
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'Fetch the data from Database.
dataGridView1.DataSource = Me.GetData("SELECT CustomerId, Name FROM Customers")
dataGridView1.AllowUserToAddRows = False
'Add a ComboBox Column to the DataGridView.
Dim comboBoxColumn As DataGridViewComboBoxColumn = New DataGridViewComboBoxColumn
comboBoxColumn.HeaderText = "Country"
comboBoxColumn.Width = 100
comboBoxColumn.Name = "comboBoxColumn"
'Loop through the DataGridView Rows.
For Each row As DataGridViewRow In dataGridView1.Rows
'Reference the ComboBoxCell.
Dim comboBoxCell As DataGridViewComboBoxCell = CType(row.Cells(2), DataGridViewComboBoxCell)
'Insert the Default Item to ComboBoxCell.
'Set the Default Value as the Selected Value.
comboBoxCell.Value = "Select Country"
'Fetch the Countries from Database.
Dim dt As DataTable = Me.GetData("SELECT CustomerId, Country from Customers")
'Loop through the DataTable Rows.
For Each drow As DataRow In dt.Rows
'Fetch the CustomerId (Primary Key) value.
Dim customerId As String = drow(0).ToString
'Add the Country value to the ComboBoxCell.
'Except for CustomerId #3.
If (customerId <> "3") Then
'Compare the value of CustomerId.
If (row.Cells(0).Value.ToString = customerId) Then
'Once CustomerId is matched, select the Country in ComboBoxCell.
comboBoxCell.Value = drow(1)
Private Function GetData(ByVal sql As String) As DataTable
Dim con As SqlConnection = New SqlConnection(ConnectionString)
Dim cmd As SqlCommand = New SqlCommand(sql, con)
cmd.CommandType = CommandType.Text
Dim sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable