Display Linq result in a DataGridView : DataGridView « LINQ « C# / C Sharp






Display Linq result in a DataGridView

 
using System.Drawing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

public class Book {
    public String Title { get; set; }

    public override String ToString() {
        return Title;
    }
}
public class FormBooks : Form {

    static public Book[] Books =
    {
      new Book {Title="F"},
      new Book {Title="B"}
    };
    public FormBooks() {
        InitializeComponent();
    }

    private void FormStrings_Load(object sender, EventArgs e) {
        String[] books = { "F", "A", "B", "R", "B" };

        var query =
          from book in books
          where book.Length > 10
          orderby book
          select new { Book = book.ToUpper() };

        dataGridView1.DataSource = query.ToList();
    }
    private void InitializeComponent() {
        this.dataGridView1 = new System.Windows.Forms.DataGridView();
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
        this.SuspendLayout();
        //
        // dataGridView1
        //
        this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.dataGridView1.Location = new System.Drawing.Point(10, 10);
        this.dataGridView1.Name = "dataGridView1";
        this.dataGridView1.Size = new System.Drawing.Size(272, 251);
        this.dataGridView1.TabIndex = 0;
        //
        // FormStrings
        //
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(292, 271);
        this.Controls.Add(this.dataGridView1);
        this.Name = "FormStrings";
        this.Padding = new System.Windows.Forms.Padding(10);
        this.Text = "FormStrings";
        this.Load += new System.EventHandler(this.FormStrings_Load);
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
        this.ResumeLayout(false);

    }
    private System.Windows.Forms.DataGridView dataGridView1;
    public static void Main() {
        Application.Run(new FormBooks());
    }
}

 








Related examples in the same category