C# DataGridView Columns - Add ComboBox Column

People love displaying data in Grids. This is especially true when it comes to desktop development in languages like C#,VB.NET and Java. In the web, people mostly display blogposts in Lists. However, in the desktop world you normally need to display alot of data like student or hospital records and these data normally need to be edited, searched and manipulated in various ways. Hence Grids do actually make a lot of sense. While Java has JTable, C# and VB.NET do have datagridview. This component is one of the most powerful and flexible winforms widgets. Not only does it allow you manipulate and edit data in different ways, it supports embeding other components like CheckBoxes, PictureBox and comboBox inside the DaTaGrirdView cells. In this lesson, we see how to embed a combobox inside the datagridview cell.

Overview

The dataGridView component allows us display data in Grids that are very flexible. The class itself belongs to System.Windows.Forms namespace, meaning it’s a winform widget. Futhermore, it resides inside the System.Windows.Forms.dll assembly. It’s a component that exists for C#, VB.NET, C++ and F#.

Questions this Examples helps answer.

  • How do we use datagridview component?
  • How do we render a combobox inside the datagridview cells?
  • How to customize datagridview to display a combobox.
  • DataGridView ComboBox column example.

Asssumptions.

We assume that you can drag a datagridview component in visual studio onto your winforms.

Screenshot

  • Here’s the screenshot of the project.
DataGridView ComboBox Column

Source Code

Lets have a look at the source code. [su_tabs] [su_tab title="Form1.cs"]
using System;
using System.Collections;
using System.Windows.Forms;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            update();
        }

        //UPDATE DGVIEW
        public void update()
        {
            //ADD COLUMNS
            dataGridView1.ColumnCount = 4;
            dataGridView1.Columns[0].Name = "Position";
            dataGridView1.Columns[1].Name = "Team";
            dataGridView1.Columns[2].Name = "Points";
            dataGridView1.Columns[3].Name = "Games";

            //ADD ROWS
            ArrayList row = new ArrayList();
            row.Add("1");
            row.Add("Chelsea");
            row.Add("83");
            row.Add("37");
            dataGridView1.Rows.Add(row.ToArray());

            //ADD 2ND ROW
            row = new ArrayList();
            row.Add("2");
            row.Add("Man City");
            row.Add("79");
            row.Add("37");
            dataGridView1.Rows.Add(row.ToArray());


            //ADD 3rd ROW
            row = new ArrayList();
            row.Add("3");
            row.Add("Man Utd");
            row.Add("76");
            row.Add("37");
            dataGridView1.Rows.Add(row.ToArray());


            //ADD 4th ROW
            row = new ArrayList();
            row.Add("4");
            row.Add("Arsenal");
            row.Add("77");
            row.Add("37");
            dataGridView1.Rows.Add(row.ToArray());

            DataGridViewLinkColumn lnk = new DataGridViewLinkColumn();
            dataGridView1.Columns.Add(lnk);
            lnk.HeaderText = "Links";
            lnk.Name = "http://fifa.com";
            lnk.Text = "http://fifa.com";
            lnk.UseColumnTextForLinkValue = true;

            ////ADD COMOBO COLUMNS
            DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
            combo.HeaderText = "Year Won";
            combo.Name = "combo";
            row = new ArrayList();
            row.Add("2015");
            row.Add("2014");
            row.Add("2013");
            row.Add("2012");
            combo.Items.AddRange(row.ToArray());
            dataGridView1.Columns.Add(combo);
        }
    }
}
 

Download

Just copy the above code.

How To Run

  1. Drag Drop a datagridview in your Form in visual studio.
  2. Copy the Form1 class above into your Form1.

More

YouTube

  • Visit our channel for more examples like these.

Facebook

Oclemy,Cheers.