In this article I will explain with an example, how to set selected value of ComboBox in DataGridView 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 item is set as selected in Windows Forms (WinForms) Application using C# and VB.Net.
Database
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.
Form Design
The Form Design consists of a DataGridView control.
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Set selected value of ComboBox in DataGridView
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.
C#
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";
dataGridView1.Columns.Add(comboBoxColumn);
//Loop through the DataGridView Rows.
foreach (DataGridViewRow row in dataGridView1.Rows)
{
//Reference the ComboBoxCell.
DataGridViewComboBoxCell comboBoxCell = (row.Cells[2] as DataGridViewComboBoxCell);
//Insert the Default Item to ComboBoxCell.
comboBoxCell.Items.Add("Select Country");
//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[0].ToString();
//Add the Country value to the ComboBoxCell.
comboBoxCell.Items.Add(drow[1]);
//Except for CustomerId #3.
if (customerId != "3")
{
//Compare the value of CustomerId.
if (row.Cells[0].Value.ToString() == customerId)
{
//Once CustomerId is matched, select the Country in ComboBoxCell.
comboBoxCell.Value = drow[1];
}
}
}
}
}
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())
{
sda.Fill(dt);
return dt;
}
}
}
}
}
VB.Net
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"
dataGridView1.Columns.Add(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.
comboBoxCell.Items.Add("Select Country")
'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.
comboBoxCell.Items.Add(drow(1))
'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)
End If
End If
Next
Next
End Sub
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
sda.Fill(dt)
Return dt
End Function
Screenshot
Downloads