Query columns from a table to be shown in a datagridview

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello Community,
I am currently using the code below in order to fill my datagridview. However, my datagridview will display columns I do not need to be seen. Also, I am using Entity Framework 5 DbContext API. DbSet<T>.ToList()
TrialEntities db = new TrialEntities();
dataGridView1.DataSource = db.Resistors.ToList();
When I use the code below to use specific columns I run into various issues with editing from another form back to the main form with a datagridview. This is because in order to refresh my datagridview I call the query again. This causes my edits to be lost, but that is not the case with the previous code above.var Qry = (from R in db.Resistors
select new
{
ResistorID = R.ResistorID,
RTC_PN = R.RTC_PN,
Description = R.Description,
KeyValue = R.KeyValue,
SymbolID = R.SymbolID
}).ToList();
dataGridView1.DataSource = Qry;
Is there a way to select the columns to use without having to select new? Should I be using something other than ".ToList" maybe? Is there a better way to go about refreshing my datagridview that wont lose my edits?
I am new with C# and any help would be greatly appreciated. Thank you!

View the full article
 
Back
Top