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. namespace WindowsFormsApplication1
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18. EmployeeDBEntities emp = new EmployeeDBEntities();
  19. private void btnForward_Click(object sender, EventArgs e)
  20. {
  21. listBox2.Items.Add(listBox1.SelectedItem);
  22. int i = 0;
  23. i = listBox1.SelectedIndex;
  24. listBox1.Items.RemoveAt(i);
  25. }
  26. private void btnBackward_Click(object sender, EventArgs e)
  27. {
  28. listBox1.Items.Add(listBox2.SelectedItem);
  29. int i = 0;
  30. i = listBox2.SelectedIndex;
  31. listBox2.Items.RemoveAt(i);
  32. }
  33. private void Form1_Load(object sender, EventArgs e)
  34. {
  35. foreach (var p in emp.Employees)
  36. {
  37. listBox1.Items.Add(p.FirstName);
  38. }
  39. }
  40. }
  41. }

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!