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

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » LINQ » DataGridViewScreenshots 
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(1010);
        this.dataGridView1.Name = "dataGridView1";
        this.dataGridView1.Size = new System.Drawing.Size(272251);
        this.dataGridView1.TabIndex = 0;
        //
        // FormStrings
        //
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(292271);
        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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.