How to link Combobox with database values in C#
By FoxLearn 11/27/2024 8:52:05 AM 6.05K
In this article, we'll walk through the process of linking a ComboBox to a database in a C# Windows Forms application. The scenario involves populating a ComboBox with categories from the Northwind database and then displaying the corresponding products for the selected category. We will use Entity Framework to interact with the database.
How to Populate a ComboBox with SQL Data in C#?
Open Visual Studio, then click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "LinkComboboxWithDatabaseValue" and then click OK
Drag and drop the Combobox, Label, DataGridView from the Visual Toolbox onto your form designer, then design your form as shown below.
Create an Entity Framework Model, then select Category and Product table from Northwind database
Linking a ComboBox with Database Values in C# Windows Forms Application
We will use the NorthwindEntities
to fetch data from the Categories
table and bind it to the ComboBox.
private void Form1_Load(object sender, EventArgs e) { // Initialize data using (NorthwindEntities db = new NorthwindEntities()) { // Fetch categories and bind them to the ComboBox categoryBindingSource.DataSource = db.Categories.ToList(); // Optionally, if the ComboBox has a selection, fetch corresponding products Category obj = cboCategory.SelectedItem as Category; if (obj != null) productBindingSource.DataSource = db.Products.Where(p => p.CategoryID == obj.CategoryID).ToList(); } }
Next, we'll handle the SelectionChangeCommitted
event to update the products based on the selected category.
private void cboCategory_SelectionChangeCommitted(object sender, EventArgs e) { // Get the selected category object Category obj = cboCategory.SelectedItem as Category; if (obj != null) { // Using Entity Framework to get products for the selected category using (NorthwindEntities db = new NorthwindEntities()) { // Get products by CategoryID productBindingSource.DataSource = db.Products.Where(p => p.CategoryID == obj.CategoryID).ToList(); } } }
When the user selects a category from the ComboBox, the cboCategory_SelectionChangeCommitted
event is triggered.
The selected item is cast to a Category
object, and the Products
table is queried to find products that match the selected CategoryID
.
The results are bound to the productBindingSource
, which in turn updates the DataGridView (dgvProducts
).
Here is the full code for the form:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace LinkComboboxWithDatabaseValue { public partial class Form1 : Form { public Form1() { InitializeComponent(); } // Event triggered when category selection changes private void cboCategory_SelectionChangeCommitted(object sender, EventArgs e) { // Get selected category from ComboBox Category obj = cboCategory.SelectedItem as Category; if (obj != null) { // Fetch products for the selected category using (NorthwindEntities db = new NorthwindEntities()) { // Query products by CategoryID and bind to the product data source productBindingSource.DataSource = db.Products.Where(p => p.CategoryID == obj.CategoryID).ToList(); } } } // Event triggered when the form loads private void Form1_Load(object sender, EventArgs e) { // Initialize data from the database using (NorthwindEntities db = new NorthwindEntities()) { // Fetch categories and bind to ComboBox categoryBindingSource.DataSource = db.Categories.ToList(); // If a category is selected, fetch and display corresponding products Category obj = cboCategory.SelectedItem as Category; if (obj != null) productBindingSource.DataSource = db.Products.Where(p => p.CategoryID == obj.CategoryID).ToList(); } } } }
VIDEO TUTORIAL