In this article I will explain with an example, how to add (insert) Please Select Option (Item) in ComboBox (DropDownList) in Windows Forms Application using C# and VB.Net.
First a DataTable will be populated from database and then the Please Select Option (Item) will be added to it, finally the ComboBox will be populated from DataTable in Windows Forms 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 consists of a ComboBox control and a Button. The Button has been assigned Click event handler.
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Add Please Select option in ComboBox
Inside the Form Load event, the records from the database are fetched into a DataTable.
Once the DataTable is populated from database, a new row is inserted in First position which will act as the Default (Blank) item for the ComboBox.
C#
private void Form1_Load(object sender, EventArgs e)
{
string constr = @"Data Source=.\SQL2014;Initial Catalog=AjaxSamples;Integrated Security=true";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlDataAdapter sda = new SqlDataAdapter("SELECT CustomerId, Name FROM Customers", con))
{
//Fill the DataTable with records from Table.
DataTable dt = new DataTable();
sda.Fill(dt);
//Insert the Default Item to DataTable.
DataRow row = dt.NewRow();
row[0] = 0;
row[1] = "Please select";
dt.Rows.InsertAt(row, 0);
//Assign DataTable as DataSource.
cbCustomers.DataSource = dt;
cbCustomers.DisplayMember = "Name";
cbCustomers.ValueMember = "CustomerId";
}
}
}
VB.Net
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Dim constr As String = "Data Source=.\SQL2014;Initial Catalog=AjaxSamples;Integrated Security=true"
Using con As SqlConnection = New SqlConnection(constr)
Using sda As SqlDataAdapter = New SqlDataAdapter("SELECT CustomerId, Name FROM Customers", con)
'Fill the DataTable with records from Table.
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
'Insert the Default Item to DataTable.
Dim row As DataRow = dt.NewRow()
row(0) = 0
row(1) = "Please select"
dt.Rows.InsertAt(row, 0)
'Assign DataTable as DataSource.
cbCustomers.DataSource = dt
cbCustomers.DisplayMember = "Name"
cbCustomers.ValueMember = "CustomerId"
End Using
End Using
End Sub
Fetching the selected Text and Value of ComboBox
When the Button is clicked, the Selected Text and Value of the ComboBox is fetched and displayed using MessageBox.
C#
private void btnSubmit_Click(object sender, EventArgs e)
{
string message = "Name: " + cbCustomers.Text;
message += Environment.NewLine;
message += "CustomerId: " + cbCustomers.SelectedValue;
MessageBox.Show(message);
}
VB.Net
Private Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmit.Click
Dim message As String = "Name: " & cbCustomers.Text
message += Environment.NewLine
message += "CustomerId: " & cbCustomers.SelectedValue
MessageBox.Show(message)
End Sub
Screenshot
Downloads