Hi smile,
Refer below sample.
SQL
CREATE TABLE Country
(
CountryId INT IDENTITY PRIMARY KEY,
CountryName VARCHAR(50)
)
Code
C#
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
CascadingEntities entities = new CascadingEntities();
List<Country> countries = entities.Countries.ToList();
comboBox1.DataSource = countries;
comboBox1.DisplayMember = "CountryName";
comboBox1.ValueMember = "CountryId";
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Name :" + comboBox1.GetItemText(comboBox1.SelectedItem) + "\nId: " + comboBox1.SelectedValue);
}
}
VB.Net
Public Class Form1
Public Sub New()
InitializeComponent()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim entities As CascadingEntities = New CascadingEntities()
Dim countries As List(Of Country) = entities.Countries.ToList()
comboBox1.DataSource = countries
comboBox1.DisplayMember = "CountryName"
comboBox1.ValueMember = "CountryId"
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
MessageBox.Show("Name :" & comboBox1.GetItemText(comboBox1.SelectedItem) & vbLf & "Id: " + comboBox1.SelectedValue)
End Sub
End Class
Screenshot