How To Populate Datagridview From Datatable In C Sharp

How To Populate Datagridview From Datatable In C#

How To Populate Datagridview From Datatable In C#

This Tutorial Shows How To Populate Datagridview From Datatable In C#

How To Populate Datagridview From Datatable In C#
How To Populate Datagridview From Datatable In C#

Form1.cs SOURCE CODE

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

namespace HowToPopulateDataGridViewFromDatatable
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //convert Image To Byte Array
        public byte[] convertImageToByteArray(Image imageToConvert)
        {
            MemoryStream ms = new MemoryStream();
            imageToConvert.Save(ms, imageToConvert.RawFormat);
            byte[] convertedImage = ms.ToArray();
            return convertedImage;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DataTable populateDataGridViewFromDatatable = new DataTable();

            //DataGridView Columns
            populateDataGridViewFromDatatable.Columns.Add("Name", typeof(string));
            populateDataGridViewFromDatatable.Columns.Add("Email", typeof(string));
            populateDataGridViewFromDatatable.Columns.Add("Phone Number", typeof(string));
            populateDataGridViewFromDatatable.Columns.Add("Language", typeof(string));
            populateDataGridViewFromDatatable.Columns.Add("Country", typeof(string));
            populateDataGridViewFromDatatable.Columns.Add("Gender", typeof(string));
            populateDataGridViewFromDatatable.Columns.Add("Image", typeof(System.Byte[]));

            //DatagridView Rows
            Image dataGridViewImage1 = Image.FromFile("C:/Users/Public/Pictures/Sample Pictures/Chrysanthemum.jpg");
            populateDataGridViewFromDatatable.Rows.Add("Joseph Derrick", "DerrickJoseph@emailextension.com", "+1999999999", "English", "United States Of America", "Male", convertImageToByteArray(dataGridViewImage1));
            Image dataGridViewImage2 = Image.FromFile("C:/Users/Public/Pictures/Sample Pictures/Desert.jpg");
            populateDataGridViewFromDatatable.Rows.Add("Joseph Derrick", "DerrickJoseph@emailextension.com", "+1999999999", "English", "United States Of America", "Male", convertImageToByteArray(dataGridViewImage2));
            Image dataGridViewImage3 = Image.FromFile("C:/Users/Public/Pictures/Sample Pictures/Hydrangeas.jpg");
            populateDataGridViewFromDatatable.Rows.Add("Joseph Derrick", "DerrickJoseph@emailextension.com", "+1999999999", "English", "United States Of America", "Male", convertImageToByteArray(dataGridViewImage3));
            Image dataGridViewImage4 = Image.FromFile("C:/Users/Public/Pictures/Sample Pictures/Jellyfish.jpg");
            populateDataGridViewFromDatatable.Rows.Add("Joseph Derrick", "DerrickJoseph@emailextension.com", "+1999999999", "English", "United States Of America", "Male", convertImageToByteArray(dataGridViewImage4));
            Image dataGridViewImage5 = Image.FromFile("C:/Users/Public/Pictures/Sample Pictures/Koala.jpg");
            populateDataGridViewFromDatatable.Rows.Add("Joseph Derrick", "DerrickJoseph@emailextension.com", "+1999999999", "English", "United States Of America", "Male", convertImageToByteArray(dataGridViewImage5));
            Image dataGridViewImage6 = Image.FromFile("C:/Users/Public/Pictures/Sample Pictures/Lighthouse.jpg");
            populateDataGridViewFromDatatable.Rows.Add("Joseph Derrick", "DerrickJoseph@emailextension.com", "+1999999999", "English", "United States Of America", "Male", convertImageToByteArray(dataGridViewImage6));
            Image dataGridViewImage7 = Image.FromFile("C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg");
            populateDataGridViewFromDatatable.Rows.Add("Joseph Derrick", "DerrickJoseph@emailextension.com", "+1999999999", "English", "United States Of America", "Male", convertImageToByteArray(dataGridViewImage7));
            
            //Increase Row Height
            dataGridView1.RowTemplate.Height = 60;
            dataGridView1.DataSource = populateDataGridViewFromDatatable;
        }
    }
}

Program.cs SOURCE CODE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace HowToPopulateDataGridViewFromDatatable
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Form1.Designer.cs SOURCE CODE

namespace HowToPopulateDataGridViewFromDatatable
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.SuspendLayout();
            // 
            // dataGridView1
            // 
            this.dataGridView1.AllowUserToAddRows = false;
            this.dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Location = new System.Drawing.Point(13, 12);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.Size = new System.Drawing.Size(826, 435);
            this.dataGridView1.TabIndex = 0;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(851, 459);
            this.Controls.Add(this.dataGridView1);
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.DataGridView dataGridView1;
    }
}


READ MORE  How To Download And Install Mozilla Firefox On Windows 10 Computer Using Internet Explorer Browser

Leave a Reply

Your email address will not be published. Required fields are marked *