none
LINQ to Entities-Northwind Windows Forms application in VC# 2008 Express: How can I display the output of the var results in DataGridView? RRS feed

  • Question

  • Hi all,

    In my Visual C# 2008 Express, I created a Windows Forms application "scWinFrmLinqToSql_Northwind", dragged and dropped a DataGridView and a BindingSource from ToolBox to the Form1, and typed in the following code:

    using System;
    
    using System.Collections.Generic;
    
    using System.ComponentModel;
    
    using System.Data;
    
    using System.Drawing;
    
    using System.Linq;
    
    using System.Text;
    
    using System.Windows.Forms;
    
    using System.Data.Linq;
    
    using System.Data.Linq.Mapping;
    
    
    
    namespace scWinFrmLingToSql_Northwind
    
    {
    
        public partial class Form1 : Form
    
        {
    
            public Form1()
    
            {
    
                InitializeComponent();
    
            }
    
    
    
            [Table]
    
            public class Customers
    
            {
    
                [Column]
    
                public string customerId;
    
                [Column]
    
                public string companyName;
    
                [Column]
    
                public string city;
    
                [Column]
    
                public string country;
    
            }
    
    
    
            private void Form1_Load(object sender, EventArgs e)
    
            {
    
                // connection string
    
                string connString = @"
    
                       server = NAB-WK-123456\SQLSCOTT;
    
                       integrated security = true;
    
                       database = northwind;
    
                       ";
    
    
    
                // create data context
    
                DataContext db = new DataContext(connString);
    
    
    
                // create typed table
    
                Table<Customers> customers = db.GetTable<Customers>();
    
    
    
                // query database
    
                var custs =
    
                    from c in customers
    
                    select
    
                       c
    
                 ;
    
    
    
                // display customers
    
                   foreach (var c in custs)
    
                   dataGridView1.DataSource = custs.ToList();
    
            }
    
        
    
        }
    
    }
    
    
    
    

    I executed this project. It ran without any error, but I just saw the Form1 with an empty DataGridView appeared. I do not know how to program to display the output of the results for the var c in custs (i. e. c.customerId, c.companyName, c.city, and c.country).   Please help and advise me how to display the output of the LINQ results for the var c in custs.

    Thanks,
    Scott Chang

     


    SHC
    • Moved by Noam Ben-Ami - MSFT1 Friday, September 18, 2009 5:59 PM (From:ADO.NET Entity Framework and LINQ to Entities)
    Friday, September 18, 2009 2:11 PM

Answers

  • Your customers object is empty.

    Declare your DataContext as so:

    public partial class MyDataBase : DataContext
    {
          // put your customers table in it     
          public Table<Customers> Customers;

          public NorthwindDb(SqlConnection conn) : base(conn) { }

    }



    Then do:

    MyDataBase db = new MyDataBase();

    var cust from c in db.Customers select c;

    // now bind it


    Formerly MichaelTx
    • Marked as answer by Yichun_Feng Friday, September 25, 2009 2:01 AM
    Friday, September 18, 2009 6:26 PM

All replies

  • Put your results into a BindingSource. Don't iterate through c just attach the result.

    BindingSource bs = new BindingSource();
    bs.DataSource = custs;

    // The attach that to your dgv
    dataGridView1.DataSource = bs;
    Formerly MichaelTx
    Friday, September 18, 2009 2:55 PM
  • Hi Michael,  Thanks for your response.

    I added the 4 code statements you gave me to the last code of my project (no error showed up). I executed the project.
    It ran, but I still got the Form1 with an empty DataGridView!!!???  Any more suggestions to this problem for me?
    Please help and advise me again.

    Thanks,
    Scott Chang
    SHC
    Friday, September 18, 2009 5:10 PM
  • Your customers object is empty.

    Declare your DataContext as so:

    public partial class MyDataBase : DataContext
    {
          // put your customers table in it     
          public Table<Customers> Customers;

          public NorthwindDb(SqlConnection conn) : base(conn) { }

    }



    Then do:

    MyDataBase db = new MyDataBase();

    var cust from c in db.Customers select c;

    // now bind it


    Formerly MichaelTx
    • Marked as answer by Yichun_Feng Friday, September 25, 2009 2:01 AM
    Friday, September 18, 2009 6:26 PM
  • Hi Scott,

     

    There are several points about your problem,

     

    1) Make sure you have the correct connect string.

    Here is an example,

    @"Data Source=CNV-MOFENG-01\SQLEXPRESS;Initial Catalog=Student;Integrated Security=True"

    Notice the highlighting element’s name is not database.

     

    2) Make the Customers class in standard format.

    Sample code:


    [Table(Name =
    Customers)]

    public class Customer

    {

    [Column(IsPrimaryKey = true)]

    public string CustomerID { get; set; }

    [Column]

    public string CompanyName { get; set; }

    [Column]

    public string Country { get; set; }

    }

     

     

    Does this works for you? If you have any questions or concerns, please update the thread and we will have a further discussion.

     

     

    Best Regards

    Yichun Feng


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, September 21, 2009 9:31 AM