Use ListView control to display data but join a reference table

user1431633

I'm really new to ASP.NET and I've looked at countless tutorials trying to figure this out but can't seem to get my head around it. I can successfully output records from tbProject using ListView and the Entity Framework but instead of the refDepartmentID, I want to display refDepartmentValue from the refDepartment table. Here is a quick table structure.

tbProject
-ProjectID
-refDepartmentID
-ProjectName

refDepartment
-refDepartmentID
-refDeparmentValue

I can write this in SQL no problem with a JOIN but I don't know where I'd put it. I've played around with LINQ a bit and it seems like this is how I'm going to accomplish this. Maybe write the LINQ in the code behind file then bind it to the ListView control, maybe?

Here's the ListView control:

<asp:ListView runat="server" 
  DataSourceID="EntityDataSource1">



  <ItemTemplate>
    <tr style="">
      <td>
        <asp:Label ID="ProjectIDLabel" runat="server" Text='<%# Eval("ProjectID") %>' />
      </td>
      <td>
        <asp:Label ID="refDepartmentIDLabel" runat="server" 
         Text='<%# Eval("refDepartmentID") %>' />
      </td>

    </tr>
   </ItemTemplate>
   <LayoutTemplate>
   <table runat="server">
     <tr runat="server">
       <td runat="server">
         <table ID="itemPlaceholderContainer" runat="server" border="0" style="">
           <tr runat="server" style="">
             <th runat="server">
               ProjectID</th>
             <th runat="server">
               refDepartmentID</th>
            </tr>
            <tr ID="itemPlaceholder" runat="server">
            </tr>
          </table>
        </td>
      </tr>
      <tr runat="server">
        <td runat="server" style="">
        </td>
      </tr>
     </table>
    </LayoutTemplate>
   </asp:ListView>

Here's how I'm using the EF to populate a dropdownlist so you can at least refer to how things are named.

// populates Departments dropdownlist
using (dbOrganizationEntities1 myEntities = new dbOrganizationEntities1())
{
    var allDepartments = from refDepartments in myEntities.refDepartments
                         select refDepartments;
    ddlDepartments.DataSource = allDepartments;
    ddlDepartments.DataValueField = "refDepartmentID";
    ddlDepartments.DataTextField = "refDepartmentValue";
    ddlDepartments.DataBind();
}

Any help would be appreciated. Thanks!

laylarenee

Disclaimer, I did this in my head without Visual Studio, so it might have a minor syntax error. Let me know if it does.

You need to do two things, First you must join your tbProject to refDepartments Entites using an inner (or outer) join as needed, depending on your desired behavior. One you have the two tables joined in LINQ, you want to create a new anonymous class using "select new" operator which creates it. The members of this new class will be created based on the datatypes of the Entities they are selected from.

using (dbOrganizationEntities1 myEntities = new dbOrganizationEntities1())
{
    var allDepartments = (from tbProject in myEntities.tbProjects
                            // inner join to department lookup table
                            from refDepartments in myEntities.refDepartments.Where(x=>x.refDepartmentID == tbProject.refDepartmentID) // to do a left join instead of an inner, append .DefaultIfEmpty() after this where clause
                            // select new anon type
                            select new {
                                refDepartmentID = tbProject.refDepartmentID,
                                ProjectName = tbProject.ProjectName,
                                refDepartmentValue = refDepartments.refDepartmentValue,
                            }).ToList(); // I chose to turn the result into a list to demonstrate something below, you can leave it as an enumerable.


    // you can access the properties of the anon type like so
    System.Diagnostics.Debug.Print(allDepartments[0].refDepartmentID);
    System.Diagnostics.Debug.Print(allDepartments[0].refDepartmentValue);

    // bind to your listview, make sure control name is accurate and ItemTemplates are defined for each data column.
    MyListView.DataSource = allDepartments;
    MyListView.DataBind();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

On click of the card control display the data into table control

From Dev

How to use Linq to join entity tables with a cross-reference table

From Dev

How to use Linq to join entity tables with a cross-reference table

From Dev

Conditional binary join and update by reference using the data.table package

From Dev

display number of rows found from data table in listview

From Dev

how to display records as empty if data not present in table using left join

From Dev

Display sqlite data on listview

From Dev

Display Filtered Data on ListView

From Dev

Display json data in listview

From Dev

Data does not display in ListView

From Dev

Join two tables with a reference table

From Dev

join self-reference table

From Dev

Mysql: how to join 2 columns from same table and use another table for reference

From Dev

Using SQL table join to use a value from one table as a reference to a value in another (primary key)

From Dev

How to Use reference table to insert rows into a data frame in R?

From Dev

How to Use reference table to insert rows into a data frame in R?

From Dev

How to use left join to take all data from master table?

From Dev

WPF display listview item in a Image control

From Dev

Join two tables and display it in a table

From Dev

Use data from an SQL query, but don't display column in table

From Dev

Use AJAX to display dictionary data returned by Django view in a table on the template

From Dev

Binding a listView to display data associated with it in another listView

From Dev

Android SQLite data display to ListView

From Dev

display echo data in listview Android

From Dev

How to display intent data in listview?

From Dev

join two tables to display data

From Dev

Display Table data in Gridview

From Dev

Display data in a Table

From Dev

Display Table data in Gridview

Related Related

  1. 1

    On click of the card control display the data into table control

  2. 2

    How to use Linq to join entity tables with a cross-reference table

  3. 3

    How to use Linq to join entity tables with a cross-reference table

  4. 4

    Conditional binary join and update by reference using the data.table package

  5. 5

    display number of rows found from data table in listview

  6. 6

    how to display records as empty if data not present in table using left join

  7. 7

    Display sqlite data on listview

  8. 8

    Display Filtered Data on ListView

  9. 9

    Display json data in listview

  10. 10

    Data does not display in ListView

  11. 11

    Join two tables with a reference table

  12. 12

    join self-reference table

  13. 13

    Mysql: how to join 2 columns from same table and use another table for reference

  14. 14

    Using SQL table join to use a value from one table as a reference to a value in another (primary key)

  15. 15

    How to Use reference table to insert rows into a data frame in R?

  16. 16

    How to Use reference table to insert rows into a data frame in R?

  17. 17

    How to use left join to take all data from master table?

  18. 18

    WPF display listview item in a Image control

  19. 19

    Join two tables and display it in a table

  20. 20

    Use data from an SQL query, but don't display column in table

  21. 21

    Use AJAX to display dictionary data returned by Django view in a table on the template

  22. 22

    Binding a listView to display data associated with it in another listView

  23. 23

    Android SQLite data display to ListView

  24. 24

    display echo data in listview Android

  25. 25

    How to display intent data in listview?

  26. 26

    join two tables to display data

  27. 27

    Display Table data in Gridview

  28. 28

    Display data in a Table

  29. 29

    Display Table data in Gridview

HotTag

Archive