How To Add Or Insert Data Or Row To Datagridview In C Sharp Windows Application

How To Display Or Show Static Data In Datagridview On Form Load In C# Net Windows Application

How To Display Or Show Static Data In Datagridview On Form Load In C# Net Windows Application
How To Add Or Insert And Display Or Show Static Rows Or Data In Datagridview
How To Add Or Insert And Display Or Show Static Rows Or Data In Datagridview

This Tutorial Shows How To Insert And Display Or Show Static Data In Datagridview On Form Load In C Sharp Net Windows Application. All rows are stored in Object arrays and added programmatically to Datagridview DataGridViewTextBox Columns. In addition to normal text rows, Images are stored in DataGridViewImage Column.

To Create/Achieve This:

  1. Create A New Form.
  2. Select Form And Set Property, Form Start Position, To Start The Form At The Center Screen.
  3. Drag And Drop A Datagrid Onto A Blank Form.
  4. Add Columns Named(And Titled): Name, Email, Phone Number, Gender, Language, Date, Image
  5. Set Auto Size Columns Mode To Fill To Prevent Some Columns From Hiding.
  6. Create Object Arrays To Hold The Datagridview Content (Data).
  7. Add Object Arrays To The Datagridview Rows.

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;

namespace HowToInsertDataIntoDataGridViewCell
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Image dataGridViewImage1 = Image.FromFile("C:/Users/Public/Pictures/Sample Pictures/Chrysanthemum.jpg");
            Object[] dataGridViewRowObject1 = new Object[] { "Hillary Johnathan", "Johnathan@gmail.com", "+1999999909", "United States Of America", "Female", "English", "20-Dec-1900", dataGridViewImage1 };
            dataGridView1.Rows.Add(dataGridViewRowObject1);
            Image dataGridViewImage2 = Image.FromFile("C:/Users/Public/Pictures/Sample Pictures/Desert.jpg");
            Object[] dataGridViewRowObject2 = new Object[] { "Hillary Johnathan", "Johnathan@gmail.com", "+1999999909", "United States Of America", "Female", "English", "20-Dec-1900", dataGridViewImage2 };
            dataGridView1.Rows.Add(dataGridViewRowObject2);
            Image dataGridViewImage3 = Image.FromFile("C:/Users/Public/Pictures/Sample Pictures/Hydrangeas.jpg");
            Object[] dataGridViewRowObject3 = new Object[] { "Hillary Johnathan", "Johnathan@gmail.com", "+1999999909", "United States Of America", "Female", "English", "20-Dec-1900", dataGridViewImage3 };
            dataGridView1.Rows.Add(dataGridViewRowObject3);
            Image dataGridViewImage4 = Image.FromFile("C:/Users/Public/Pictures/Sample Pictures/Jellyfish.jpg");
            Object[] dataGridViewRowObject4 = new Object[] { "Hillary Johnathan", "Johnathan@gmail.com", "+1999999909", "United States Of America", "Female", "English", "20-Dec-1900", dataGridViewImage4 };
            dataGridView1.Rows.Add(dataGridViewRowObject4);
            Image dataGridViewImage5 = Image.FromFile("C:/Users/Public/Pictures/Sample Pictures/Koala.jpg");
            Object[] dataGridViewRowObject5 = new Object[] { "Hillary Johnathan", "Johnathan@gmail.com", "+1999999909", "United States Of America", "Female", "English", "20-Dec-1900", dataGridViewImage5 };
            dataGridView1.Rows.Add(dataGridViewRowObject5);
            Image dataGridViewImage6 = Image.FromFile("C:/Users/Public/Pictures/Sample Pictures/Lighthouse.jpg");
            Object[] dataGridViewRowObject6 = new Object[] { "Hillary Johnathan", "Johnathan@gmail.com", "+1999999909", "United States Of America", "Female", "English", "20-Dec-1900", dataGridViewImage6 };
            dataGridView1.Rows.Add(dataGridViewRowObject6);
            Image dataGridViewImage7 = Image.FromFile("C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg");
            Object[] dataGridViewRowObject7 = new Object[] { "Hillary Johnathan", "Johnathan@gmail.com", "+1999999909", "United States Of America", "Female", "English", "20-Dec-1900", dataGridViewImage7 };
            dataGridView1.Rows.Add(dataGridViewRowObject7);
            Image dataGridViewImage8 = Image.FromFile("C:/Users/Public/Pictures/Sample Pictures/Tulips.jpg");
            Object[] dataGridViewRowObject8 = new Object[] { "Hillary Johnathan", "Johnathan@gmail.com", "+1999999909", "United States Of America", "Female", "English", "20-Dec-1900", dataGridViewImage8 };
            dataGridView1.Rows.Add(dataGridViewRowObject8);
            
        }
    }
}

Form1.Designer.cs SOURCE CODE

namespace HowToInsertDataIntoDataGridViewCell
{
    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();
            this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column3 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column4 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column5 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column6 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column7 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column8 = new System.Windows.Forms.DataGridViewImageColumn();
            ((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.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.Column1,
            this.Column2,
            this.Column3,
            this.Column4,
            this.Column5,
            this.Column6,
            this.Column7,
            this.Column8});
            this.dataGridView1.Location = new System.Drawing.Point(2, 1);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.Size = new System.Drawing.Size(789, 290);
            this.dataGridView1.TabIndex = 0;
            // 
            // Column1
            // 
            this.Column1.HeaderText = "Full Name";
            this.Column1.Name = "Column1";
            // 
            // Column2
            // 
            this.Column2.HeaderText = "Email";
            this.Column2.Name = "Column2";
            // 
            // Column3
            // 
            this.Column3.HeaderText = "Phone Number";
            this.Column3.Name = "Column3";
            // 
            // Column4
            // 
            this.Column4.HeaderText = "Country";
            this.Column4.Name = "Column4";
            // 
            // Column5
            // 
            this.Column5.HeaderText = "Gender";
            this.Column5.Name = "Column5";
            // 
            // Column6
            // 
            this.Column6.HeaderText = "Language";
            this.Column6.Name = "Column6";
            // 
            // Column7
            // 
            this.Column7.HeaderText = "Date Of Birth";
            this.Column7.Name = "Column7";
            // 
            // Column8
            // 
            this.Column8.HeaderText = "Image";
            this.Column8.ImageLayout = System.Windows.Forms.DataGridViewImageCellLayout.Stretch;
            this.Column8.Name = "Column8";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(794, 294);
            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;
        private System.Windows.Forms.DataGridViewTextBoxColumn Column1;
        private System.Windows.Forms.DataGridViewTextBoxColumn Column2;
        private System.Windows.Forms.DataGridViewTextBoxColumn Column3;
        private System.Windows.Forms.DataGridViewTextBoxColumn Column4;
        private System.Windows.Forms.DataGridViewTextBoxColumn Column5;
        private System.Windows.Forms.DataGridViewTextBoxColumn Column6;
        private System.Windows.Forms.DataGridViewTextBoxColumn Column7;
        private System.Windows.Forms.DataGridViewImageColumn Column8;
    }
}


Program.cs SOURCE CODE

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

namespace HowToInsertDataIntoDataGridViewCell
{
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());
}
}
}

READ MORE  The type or namespace name 'MySql' could not be found (are you missing a using directive or an assembly reference)

Leave a Reply

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