Custom DataGridView with DataGridViewTextBoxColumn, DataGridViewImageColumn,DataGridViewComboBoxColumn : DataGridView « Database ADO.net « C# / C Sharp






Custom DataGridView with DataGridViewTextBoxColumn, DataGridViewImageColumn,DataGridViewComboBoxColumn

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;

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

    private void getData_Click(object sender, EventArgs e) {
        getData.Enabled = false;
        using (SqlConnection con = new SqlConnection(ConfigurationSettings.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);
            dataGridView.AutoGenerateColumns = false;
            dataGridView.DataSource = ds.Tables["Employees"];
            dataGridView.AutoSizeRows(DataGridViewAutoSizeRowsMode.HeaderAndColumnsAllRows);
        }
    }

    private void SetupColumns(DataSet ds) {
        DataGridViewTextBoxColumn forenameColumn = new DataGridViewTextBoxColumn();
        forenameColumn.DataPropertyName = "FirstName";
        forenameColumn.HeaderText = "Forename";
        forenameColumn.ValueType = typeof(string);
        forenameColumn.Frozen = true;
        dataGridView.Columns.Add(forenameColumn);

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

        DataGridViewImageColumn photoColumn = new DataGridViewImageColumn();
        photoColumn.DataPropertyName = "Photo";
        photoColumn.Width = 200;
        photoColumn.HeaderText = "Image";
        photoColumn.ReadOnly = true;
        photoColumn.ImageLayout = DataGridViewImageCellLayout.Normal;
        dataGridView.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";
        dataGridView.Columns.Add(reportsToColumn);

    }
    private void InitializeComponent() {
        this.getData = new System.Windows.Forms.Button();
        this.dataGridView = new System.Windows.Forms.DataGridView();
        this.SuspendLayout();
        // 
        // getData
        // 
        this.getData.Location = new System.Drawing.Point(661, 544);
        this.getData.Name = "getData";
        this.getData.TabIndex = 0;
        this.getData.Text = "Get Data";
        this.getData.Click += new System.EventHandler(this.getData_Click);
        // 
        // dataGridView
        // 
        this.dataGridView.AllowUserToAddRows = false;
        this.dataGridView.AllowUserToDeleteRows = false;
        this.dataGridView.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.dataGridView.Location = new System.Drawing.Point(13, 13);
        this.dataGridView.Name = "dataGridView";
        this.dataGridView.Size = new System.Drawing.Size(722, 522);
        this.dataGridView.TabIndex = 1;
        // 
        // Form1
        // 
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(748, 579);
        this.Controls.Add(this.dataGridView);
        this.Controls.Add(this.getData);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);
    }
    private System.Windows.Forms.Button getData;
    private System.Windows.Forms.DataGridView dataGridView;

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

           
       








Related examples in the same category

1.Bind DataGridView to Array
2.Data Source with Generic Collection
3.Setup Columns for DataGridView
4.Bind List to DataGridView
5.Set up DataGridView from DataColumn
6.Use DataViewRowState to filter DataGridView