In this example, we will learn How to Create Datagridview Image Column And Add Images To The Column In C# Windows Form Application.
We will fill the datagridview with the Country Names with the flags.
Step 1: Create Datagridview image column.
1 2 3 |
DataGridViewImageColumn dgvImageColumn = new DataGridViewImageColumn(); |
Step 2: Set header text to the Image column.
1 2 3 |
dgvImageColumn.HeaderText = "Image"; |
Step 3: Display the entire image.
1 2 3 |
dgvImageColumn.ImageLayout = DataGridViewImageCellLayout.Stretch; |
Step 4: Create Datagridview text column
1 2 3 |
DataGridViewTextBoxColumn dgvCountryColumn = new DataGridViewTextBoxColumn(); |
Step 5: Set header text to the Text column.
1 2 3 |
dgvCountryColumn.HeaderText = "Country"; |
Step 6: Create and add images to the dataGridView.
1 2 3 4 5 |
Image img1; img1 = Image.FromFile(@"D:\flags\Albania-Flag-icon.png"); dataGridView1.Rows.Add("ALBANIA", img1); |
All Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace image_datagridview { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.Text = "www.csharp-console-examples.com"; this.BackColor = Color.Bisque; DataGridViewImageColumn dgvImageColumn = new DataGridViewImageColumn(); dgvImageColumn.HeaderText = "Image"; dgvImageColumn.ImageLayout = DataGridViewImageCellLayout.Stretch; DataGridViewTextBoxColumn dgvCountryColumn = new DataGridViewTextBoxColumn(); dgvCountryColumn.HeaderText = "Country"; dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; dataGridView1.RowTemplate.Height = 80; dataGridView1.AllowUserToAddRows = false; dataGridView1.Columns.Add(dgvCountryColumn); dataGridView1.Columns.Add(dgvImageColumn); Image img1; img1 = Image.FromFile(@"D:\flags\Albania-Flag-icon.png"); dataGridView1.Rows.Add("ALBANIA", img1); Image img2; img2 = Image.FromFile(@"D:\flags\Argentina-Flag-icon.png"); dataGridView1.Rows.Add("ARGENTINA", img2); Image img3; img3 = Image.FromFile(@"D:\flags\Australia-Flag-icon.png"); dataGridView1.Rows.Add("AUSTRALIA", img3); Image img4; img4 = Image.FromFile(@"D:\flags\Belgium-Flag-icon.png"); dataGridView1.Rows.Add("BELGIUM", img4); } } } |
Output: