How to fill ComboBox and DataGridView automatically in C#
By FoxLearn 12/1/2024 2:44:54 PM 6.85K
In this article, we will explore how to automatically populate both a ComboBox
and a DataGridView
in a C# Windows Forms application using Entity Framework.
How to Automatically Populate ComboBox and DataGridView 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 "DataGridViewAndCombobox" and then click OK
Drag and drop the Label, Combobox and DataGridView controls form 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
When the form is loaded, we use Entity Framework
to connect to the NorthwindEntities
database context.
private void Form1_Load(object sender, EventArgs e) { try { // Initialize and bind ComboBox data from the database using (NorthwindEntities db = new NorthwindEntities()) { db.Configuration.ProxyCreationEnabled = false; // Bind categories to ComboBox cboCategory.DataSource = db.Categories.ToList(); cboCategory.ValueMember = "CategoryID"; // The field that represents the value cboCategory.DisplayMember = "CategoryName"; // The field that represents the display text } } catch (Exception ex) { MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
We retrieve the list of categories from the Categories
table using LINQ (db.Categories.ToList()
), then we bind the ComboBox
(cboCategory
) with the CategoryName
as the display text and CategoryID
as the value.
When the user selects a category from the ComboBox
, we get the selected Category
object.
private void cboCategory_SelectionChangeCommitted(object sender, EventArgs e) { // Get the selected category Category obj = cboCategory.SelectedItem as Category; if (obj != null) { Cursor.Current = Cursors.WaitCursor; try { using (NorthwindEntities db = new NorthwindEntities()) { db.Configuration.ProxyCreationEnabled = false; // Use LINQ to fetch products based on the selected category var query = from o in db.Products where o.CategoryID == obj.CategoryID select o; // Bind the result to the DataGridView dataGridView.DataSource = query.ToList(); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error); } Cursor.Current = Cursors.Default; } }
We use the CategoryID
to query the Products
table for products that belong to the selected category. The LINQ
query fetches the products and binds them to the DataGridView
.
The ComboBox
is populated with categories, and the DataGridView
is populated with products from the selected category.
In this tutorial, we demonstrated how to automatically fill both a ComboBox
and a DataGridView
in a C# Windows Forms application using Entity Framework.
VIDEO TUTORIAL
- How to Print Text in a Windows Form Application Using C#
- How to Read text file and Sort list in C#
- How to pass ListView row data into another Form in C#
- How to read and write to text file in C#
- How to make a Countdown Timer in C#
- How to Display selected Row from DataGridView to TextBox in C#
- How to Get all Forms and Open Form with Form Name in C#
- How to Get Checked Items In a CheckedListBox in C#