Download Example with simulation(fake table) of table:
mediafire[.]com/file/4f6b52yyovlu4en/demo.zip
Code
namespace demo
{
public partial class Form1 : Form
{
private List<DemoClass> fakeTable = new List<DemoClass>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
fakeTable.Add(new DemoClass() { ID = 1, Name = "Hack", Lastname = "Forums" });
fakeTable.Add(new DemoClass() { ID = 1, Name = "Hack 1", Lastname = "Forums 1" });
fakeTable.Add(new DemoClass() { ID = 2, Name = "Hack 2", Lastname = "Forums 2" });
fakeTable.Add(new DemoClass() { ID = 2, Name = "Hack 23", Lastname = "Forums 23" });
fakeTable.Add(new DemoClass() { ID = 3, Name = "Hack 3", Lastname = "Forums 3" });
LoadData(dgvDemo);
}
private void LoadData(DataGridView dgvDemo)
{
dgvDemo.DataSource=fakeTable.ToList();
}
private void Filter(ComboBox cmbDemo, DataGridView dgv)
{
switch (cmbDemo.SelectedIndex)
{
case 0:
dgv.DataSource = fakeTable.Where(p => p.ID==p.ID).ToList();
break;
case 1:
dgv.DataSource = fakeTable.Where(p => p.ID == 2).ToList();
break;
case 2:
dgv.DataSource = (from t1 in fakeTable where t1.ID==2 select t1).ToList();
break;
case 3:
dgv.DataSource = (from t1 in fakeTable where t1.ID == 2 select new { t1.ID,t1.Name}).ToList();
break;
case 4:
dgv.DataSource = (from t1 in fakeTable where t1.ID == 2 select new { t1.ID, t1.Name,t1.Lastname }).ToList();
break;
case 5:
dgv.DataSource = null;
dgv.DataSource = fakeTable.Select(p => p).ToList();
break;
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Filter(comboBox1,dgvDemo);
}
}
}
you write well encapsulation,your code seems to have written ok although I thought it was not in my first post..sorry for that... so I made a little demo to check...What can happen to the project if it is to do in MV or project that is separate from the database leyer in anather projects must copy connection string in bouth project in some case or try to update EF from v5 to v6 over nuget and reload EF in project...If none of the above help shere app config with us.