Read last name of the first person in the table
BasicsEntities m_context = new BasicsEntities();
..
IQueryable<person> personsQuery = m_context.CreateObjectSet<person>();
string lastName = personsQuery.First().lastName;
More info and samples on: www.devarchweb.net
Query/join data from Persons and Departments tablesIQueryable<person> personsWithDepartmentsQuery = m_context.CreateObjectSet<person>().Include("department");
string deptName = personsWithDepartmentsQuery.First().department.name;
More info and samples on: www.devarchweb.net
Use LINQ syntax to retrive data from Persons tableIQueryable query = from p in m_context.persons
select p;
More info and samples on: www.devarchweb.net
Display list of last names in Listboxusing System.Data.Objects; // System.Data.Entity.dll
...
IQueryable query = from p in m_context.persons select p;
listBox1.DisplayMember = "LastName";
listBox1.DataSource = ((ObjectQuery) query).Execute(MergeOption.AppendOnly);
More info and samples on: www.devarchweb.net
Add personperson person1 = new person { firstName = "Zbynek", lastName = "Cernin", department_id = 1 };
m_context.persons.AddObject(person1);
m_context.SaveChanges();
More info and samples on: www.devarchweb.net
Delete last personm_context.persons.DeleteObject(m_context.persons.Last());
m_context.SaveChanges();
More info and samples on: www.devarchweb.net
Read Persons table into DataGridView and save changes made by user - EF6// load data
IQueryable query = from pers in m_context.persons
select pers;
dataGridView1.DataSource = ((ObjectQuery)query).Execute(MergeOption.AppendOnly);
// or based on you data model
// dataGridView1.DataSource = ((System.Data.Entity.Infrastructure.DbQuery<Person>>)query).ToList();
//Save changes after edit
dataGridView1.EndEdit();
m_context.SaveChanges();
More info and samples on: www.devarchweb.net
Delete multiple selected rows DataGridView - EF6// EF 6
// code for a button event handler
// delete rows in database
foreach (var row in dataGridView1.SelectedRows)
{
object o = ((DataGridViewRow)row).Cells[ID_INDEX].Value;
int id = Convert.ToInt32(o); // (int)o throws InvalidCastException
_dbContext.Persons.Remove(_dbContext.ElementExtern.Where(el => el.Id == id).First());
}
_dbContext.SaveChanges();
// delete rows in Grid
for (int i = dataGridView1.Rows.Count - 1; i >= 0; i--)
{
if (dataGridView1.Rows[i].Selected)
{
dataGridView1.Rows.RemoveAt(i); // dataGridView1.DataSource must be bound to BindingSource in order to allow RemoveAt() to run
}
}
More info and samples on: www.devarchweb.net
Load, edit and save data from 2 tables (Persons, Department) in one DataGridView - EF6 In order to achieve this you need to create a helper class that will hold references to original entities public class QueryData
{
public Person Person;
public Department Department;
public long Id
{
get { return Person.Id; }
}
public string Name
{
get { return Person.Name; }
set { Person.Name = value; }
}
public string DepartmentName
{
get { return Department.Name; }
set { Department.Name = value; }
}
}
var query = from p in _dbContext.Persons
join d in _dbContext.Department on p.DepartmentId equals d.Id into d1
from d2 in d1.DefaultIfEmpty()
select new QueryData { Person = p, Department = d2 };
BindingSource bs = new BindingSource(); // without BindingSource it is not possible to call: dataGridView1.Rows.RemoveAt(i);
bs.DataSource = query.ToList();
dataGridView1.DataSource = bs;
dataGridView1.Refresh();
}
More info and samples on: www.devarchweb.net
Use combo box for enumerated values in DataGridView You need a class that represents internal data (e.g. value in DB) and text presented to the userpublic class Gender
{
public int Code { get; set;}
public string Name { get; set; }
}
List>Gender< genders = new List>Gender<();
genders.Add(new ElementType() { TypeCode = "0", Name = "Female" });
genders.Add(new ElementType() { TypeCode = "1", Name = "Male" });
DataGridViewComboBoxColumn columnType = new DataGridViewComboBoxColumn();
columnType.DataPropertyName = "Gender"; // name of the public property in the class that is datasource for the DataGridView (QueryData in the example above)
columnType.DataSource = genders;
columnType.ValueMember = "TypeCode";
columnType.DisplayMember = "Name";
dataGridView1.Columns.Insert(2, columnType);