1

I have the following code in a C# Visual Studio Web App:

SampleDataContext dbContext = new SampleDataContext();
gridView1.DataSource = from employee in dbContext.Employees
                                       select employee;

This code works fine to display the table "Employee" in the SQL database "Sample".

I want to use similar code in a Windows Form App, using the DataGridView element instead of the GridView element:

SampleDataContext dbContext = new SampleDataContext();
dataGridView1.DataSource = from employee in dbContext.Employees
                                       select employee;

This code mostly works but it also displays an extra column, I believe because the data has a foreign key. Every row reads "[Project Name].[Table that foreign key points to]" under the extra column named "[Table that foreign key points to]" How do I make it so that this column does not appear?

Do I have to load the table into a DataTable element and then use that as the DataSource for DataGridView element?

Chase
  • 11
  • 2

1 Answers1

2

Here is an example that shows using lambda againsts an Employee class and one using projections. Top DataGridView uses a projection againsts this model.

Projection

public class Employee
{
    public int EmployeeId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Title { get; set; }
    public override string ToString() => $"{Title} {FirstName} {LastName}";
    /// <summary>
    /// This projection simplifies a lambda select
    /// </summary>
    public static Expression<Func<Employees, Employee>> Projection =>
        employee => new Employee()
        {
            EmployeeId = employee.EmployeeId,
            FirstName = employee.FirstName,
            LastName = employee.LastName,
            Title = employee.Title
            
        };
}

These methods (located in this class) read data which in turn will be used to populate two DataGridView controls as per the screenshot.

public static async Task<List<Employee>> EmployeesProjected()
{

    return await Task.Run(async () =>
    {
        List<Employee> customerItemsList = await Context.Employees
            .Select(Employee.Projection)
            .ToListAsync();

        return customerItemsList;
    });

}

public static List<Employees> Employees()
{
    return Context.Employees.ToList();
}

Here is the form code

public partial class EmployeesForm : Form
{
    public EmployeesForm()
    {
        InitializeComponent();
        Shown += EmployeesForm_Shown;
    }

    private async void EmployeesForm_Shown(object sender, EventArgs e)
    {
        var employees = await Operations.EmployeesProjected();
        EmployeeProjectedDataGridView.DataSource = employees;

        EmployeeDataGridView.DataSource = Operations.Employees();
        EmployeeDataGridView.Columns["Orders"].DisplayIndex = 4;
    }
}

Only other option is to create DataGridViewColumns and set their DataPropertyName which then excludes child tables (also known as navigation properties).

enter image description here

Karen Payne
  • 4,341
  • 2
  • 14
  • 31