Fill a DataGrid : DataGrid « Database ADO.net « 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 » Database ADO.net » DataGridScreenshots 
Fill a DataGrid
 
using System;
using System.Diagnostics;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

public class Form1 : System.Windows.Forms.Form {
    internal System.Windows.Forms.DataGrid DataGrid1;
    internal System.Windows.Forms.Button btnRunQuery;
    private System.Windows.Forms.Button btnRunQuery2;
    public Form1() {
        this.DataGrid1 = new System.Windows.Forms.DataGrid();
        this.btnRunQuery = new System.Windows.Forms.Button();
        this.btnRunQuery2 = new System.Windows.Forms.Button();
        ((System.ComponentModel.ISupportInitialize)(this.DataGrid1)).BeginInit();
        this.SuspendLayout();

        this.DataGrid1.DataMember = "";
        this.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
        this.DataGrid1.Location = new System.Drawing.Point(88);
        this.DataGrid1.Size = new System.Drawing.Size(280200);

        this.btnRunQuery.Location = new System.Drawing.Point(8224);
        this.btnRunQuery.Size = new System.Drawing.Size(8024);
        this.btnRunQuery.Text = "Run Query 1";
        this.btnRunQuery.Click += new System.EventHandler(this.btnRunQuery_Click);

        this.btnRunQuery2.Location = new System.Drawing.Point(104224);
        this.btnRunQuery2.Size = new System.Drawing.Size(8024);
        this.btnRunQuery2.Text = "Run Query 2";
        this.btnRunQuery2.Click += new System.EventHandler(this.btnRunQuery2_Click);

        this.AutoScaleBaseSize = new System.Drawing.Size(513);
        this.ClientSize = new System.Drawing.Size(292273);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.btnRunQuery2,
                                                                          this.DataGrid1,
                                                                          this.btnRunQuery});
        ((System.ComponentModel.ISupportInitialize)(this.DataGrid1)).EndInit();
        this.ResumeLayout(false);

    }
    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }

    private void btnRunQuery_Click(object sender, System.EventArgs e) {
        try {
            SqlConnection cn = new SqlConnection("data source=.;database=biblio;uid=admin;pwd=pw");

            SqlDataAdapter da = new SqlDataAdapter("Select top 50 Author, Year_born from Authors Where Year_born is not null", cn);

            DataSet ds = new DataSet();

            da.Fill(ds, "Authors");

            DataGrid1.DataSource = ds.Tables["Authors"];
        catch (SqlException ex) {
            Debug.WriteLine(ex.ToString());
        }
    }

    private void btnRunQuery2_Click(object sender, System.EventArgs e) {
        try {
            SqlConnection cn = new SqlConnection("data source=.;database=biblio;uid=admin;pwd=pw");

            SqlDataAdapter da = new SqlDataAdapter("Select Title, Price from Titles where Title like 'Hit%'", cn);

            DataSet ds = new DataSet();

            da.Fill(ds, "Titles and Price");

            DataGrid1.DataSource = ds.Tables["Titles and Price"];
        catch (SqlException ex) {
            Debug.WriteLine(ex.ToString());
        }
    }

}

 
Related examples in the same category
1.Set SelectCommand
2.Bind related tables
3.Set Data Binding with DataViewManager
4.Set up relation between two tables
5.Set DataSource from DataSet
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.