The problem with the line:
select new { c.UserName, c.Password, c.Description }
Is that it is creating an anonymous type, and anonymous types are immutable - that is read only. This is why your changes do not get reflected in either the new type or in the original EF object.
Now, as for ways of not showing all the columns of the object you are binding to, I've given three options below.
Hide unwanted columns
The most straight forward approach is to set the visible property to false for the columns you do not want to show.
dataGridView1.Columns[0].Visible = false;
Where the value in the Columns collection indexer can either be an integer specifying the column location or a string for the column's name.
Custom object in EF for this databinding
You could also handle this at the EF layer - creating a custom object for your binding which EF maps from the database without the columns you don't want. I haven't used EF 4.0 at all really but I understand that it has this capability now.
Custom DTO projected from EF object and then mapped back
The third option (and these are going from good to bad in my opinion of them but I thought I'd tell you a few approaches!) is to query to a concrete type and then map back to the EF object. Something like:
private class DataBindingProjection
{
public string UserName { get; set; };
public string Password { get; set; };
public string Description { get; set; };
}
private void simpleButton1_Click(object sender, EventArgs e)
{
context = new WS_Entities();
var query = from c in context.Users
where c.UserName == "James"
select new DataBindingProjection { UserName = c.UserName, Password = c.Password, Description = c.Description };
var users = query.ToList();
gridControl1.DataSource = users;
}
private void simpleButton2_Click(object sender, EventArgs e)
{
// and here you have some code to map the properties back from the
// projection objects to your datacontext
context.SaveChanges();
}
In certain situations that could be a workable solution too...
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…