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.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.