Fill ListBox with ListBoxItems using Entity Framework

Introduction

In this blog we will see how we can fill ListBox with ListBoxItems using entity framework.

Step 1: Create ASP.NET Web Application

Form1.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10.   
  11. namespace WindowsFormsApplication1  
  12. {  
  13.     public partial class Form1 : Form  
  14.     {  
  15.         public Form1()  
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.   
  20.         EmployeeDBEntities emp = new EmployeeDBEntities();  
  21.         private void btnForward_Click(object sender, EventArgs e)  
  22.         {  
  23.             listBox2.Items.Add(listBox1.SelectedItem);  
  24.             int i = 0;  
  25.             i = listBox1.SelectedIndex;  
  26.             listBox1.Items.RemoveAt(i);  
  27.         }  
  28.   
  29.         private void btnBackward_Click(object sender, EventArgs e)  
  30.         {  
  31.             listBox1.Items.Add(listBox2.SelectedItem);  
  32.             int i = 0;  
  33.             i = listBox2.SelectedIndex;  
  34.             listBox2.Items.RemoveAt(i);  
  35.         }  
  36.   
  37.         private void Form1_Load(object sender, EventArgs e)  
  38.         {  
  39.   
  40.             foreach (var p in emp.Employees)  
  41.             {  
  42.                 listBox1.Items.Add(p.FirstName);  
  43.             }  
  44.         }  
  45.     }  
  46. }  

Output of the application looks like this

Summary

In this blog we have seen how the listbox can be filled with listboxitems using entity framework. Happy coding!

MVC Corporation
MVC Corporation is consulting and IT services based company.