6699

How to bind a query with a join to a DataGridView?

Question:

Right now, I have created a new Object data source based off from an Object Context from the entity model. I then created a BindingSource and a DataGridView set to this BindingSource.

I can add columns which are bound to the data from the TraceLine table. When I set the DataSource, I see values in those columns. However, I can’t seem to get the data from the joined table. How do I bind a DataGridView to a query that has a join?

using (var entities = new MyEntities()) { var lines = from t in entities.Lines join m in entities.Methods on t.MethodHash equals m.MethodHash where t.UserSessionProcessId == m_SessionId select new { m.Name, // doesn't get displayed in DataGridView, but I want it to t.Sequence, t.InclusiveDuration, t.ExclusiveDuration }; dgvBindingSource.DataSource = lines; }

Answer1:

One possible issue is that the DataGridView may have its DataSource set at design time to one of the types but at runtime you're setting it to an anonymous type that has an extra member. If I recall, DataGridView won't re-generate the columns if you change the data source after columns have been generated.

You may need to set the data source to null, clear the column collection, then set the data source. In fact a better idea would be to create the columns explicitly instead of auto generating them.

Recommend

  • Are there any possible ways to ignore all paths of JPA Example Matcher
  • sql query Update INNER JOINED table
  • Executing merge statement via C# SqlCommand not working
  • PyQt - Load SQL in QAbstractTableModel (QTableView) using pandas DataFrame - editing datas in a GUI
  • Get Users in Group from Azure AD via Microsoft Graph
  • Why == is different for Integer and String?
  • Where should I store a file in Android?
  • Dynamically generated lookup key for IQueryable
  • How to fail Phing without triggering backtrace
  • Jhipster: How to create relationships with User entity using supplied tools?
  • JPA/EclipseLink Returning No Results
  • Linq Full Outer Join on Two Objects
  • How to get the index of element in the List in c#
  • Is is safe to use HSQLDB for production? (JBoss AS5.1)
  • Is there a way to disable a specific event in kendo ui scheduler?
  • Insertion large number of Entities into SQL Server 2012 [duplicate]
  • Knockout custom binding handler
  • Force show.bind execution
  • Time complexity of a program which involves multiple variables
  • Can you perform a UNION without a subquery in SQLAlchemy?
  • Word Open XML Mail Merge
  • How do I alternate colors in Flat List (React Native)
  • PostgreSQL Query without WHERE only ORDER BY and LIMIT doesn't use index
  • Django: Count of Group Elements
  • jQuery show() function is not executed in Safari if submit handler returns true
  • How to check if every primary key value is being referenced as foreign key in another table
  • How to handle AllServersUnavailable Exception
  • Convert array of 8 bytes to signed long in C++
  • Display Images one by one with next and previous functionality
  • How to get next/previous record number?
  • How to delete a row from a dynamic generate table using jquery?
  • SQL merge duplicate rows and join values that are different
  • using HTMLImports.whenReady not working in chrome
  • Understanding cpu registers
  • How to get Windows thread pool to call class member function?
  • Authorize attributes not working in MVC 4
  • EntityFramework adding new object to nested object collection
  • Is there any way to bind data to data.frame by some index?
  • Recursive/Hierarchical Query Using Postgres
  • Running Map reduces the dimensions of the matrices