Setup Columns for DataGridView : DataGridView « 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.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 » Database ADO.net » DataGridViewScreenshots 
Setup Columns for DataGridView




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Configuration;
using System.Data.SqlClient;

// CustomDataGridView

public class Form1 : Form {
    public Form1() {
        InitializeComponent();
    }

    private void getData_Click(object sender, EventArgs e) {
        getData.Enabled = false;

        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["northwind"].ConnectionString)) {
            string select "SELECT EmployeeID, FirstName, LastName, Photo, IsNull(ReportsTo,0) as ReportsTo FROM Employees";

            SqlDataAdapter da = new SqlDataAdapter(select, con);

            DataSet ds = new DataSet();

            da.Fill(ds, "Employees");

            select "SELECT EmployeeID, FirstName + ' ' + LastName as Name FROM Employees union select 0,'(None)'";

            da = new SqlDataAdapter(select, con);
            da.Fill(ds, "Managers");
            SetupColumns(ds);
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = ds.Tables["Employees"];
            dataGridView1.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCells);
        }
    }
    private void SetupColumns(DataSet ds) {
        DataGridViewTextBoxColumn forenameColumn = new DataGridViewTextBoxColumn();
        forenameColumn.DataPropertyName = "FirstName";
        forenameColumn.HeaderText = "Forename";
        forenameColumn.ValueType = typeof(string);
        forenameColumn.Frozen = true;
        dataGridView1.Columns.Add(forenameColumn);

        DataGridViewTextBoxColumn surnameColumn = new DataGridViewTextBoxColumn();
        surnameColumn.DataPropertyName = "LastName";
        surnameColumn.HeaderText = "Surname";
        surnameColumn.Frozen = true;
        surnameColumn.ValueType = typeof(string);
        dataGridView1.Columns.Add(surnameColumn);

        DataGridViewImageColumn photoColumn = new DataGridViewImageColumn();
        photoColumn.DataPropertyName = "Photo";
        photoColumn.Width = 200;
        photoColumn.HeaderText = "Image";
        photoColumn.ReadOnly = true;
        photoColumn.ImageLayout = DataGridViewImageCellLayout.Normal;
        dataGridView1.Columns.Add(photoColumn);

        DataGridViewComboBoxColumn reportsToColumn = new DataGridViewComboBoxColumn();
        reportsToColumn.HeaderText = "Reports To";
        reportsToColumn.DataSource = ds.Tables["Managers"];
        reportsToColumn.DisplayMember = "Name";
        reportsToColumn.ValueMember = "EmployeeID";
        reportsToColumn.DataPropertyName = "ReportsTo";
        dataGridView1.Columns.Add(reportsToColumn);

    }
    private void InitializeComponent() {
        this.dataGridView1 = new System.Windows.Forms.DataGridView();
        this.getData = new System.Windows.Forms.Button();
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
        this.SuspendLayout();
        // 
        // dataGridView1
        // 
        this.dataGridView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                    | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.dataGridView1.Location = new System.Drawing.Point(1313);
        this.dataGridView1.Name = "dataGridView1";
        this.dataGridView1.Size = new System.Drawing.Size(507372);
        this.dataGridView1.TabIndex = 0;
        // 
        // getData
        // 
        this.getData.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
        this.getData.Location = new System.Drawing.Point(445391);
        this.getData.Name = "getData";
        this.getData.Size = new System.Drawing.Size(7523);
        this.getData.TabIndex = 1;
        this.getData.Text = "Get Data";
        this.getData.UseVisualStyleBackColor = true;
        this.getData.Click += new System.EventHandler(this.getData_Click);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(532426);
        this.Controls.Add(this.getData);
        this.Controls.Add(this.dataGridView1);
        this.Name = "Form1";
        this.Text = "CustomDataGridView";
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
        this.ResumeLayout(false);

    }



    private System.Windows.Forms.DataGridView dataGridView1;
    private System.Windows.Forms.Button getData;

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

           
       
Related examples in the same category
1.Bind DataGridView to Array
2.Data Source with Generic Collection
3.Bind List to DataGridView
4.Custom DataGridView with DataGridViewTextBoxColumn, DataGridViewImageColumn,DataGridViewComboBoxColumn
5.Set up DataGridView from DataColumn
6.Use DataViewRowState to filter DataGridView
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.