Search code examples
c#winformsdatagridviewdatagridviewcomboboxcolumn

DataGridViewComboBoxColumn displaying "DataGridViewComboBoxColumn { Name=NewBox, Index=-1 }" text instead of ComboBox


I am making a simple windows forms application that imports the headers from a CSV file into a DataGridView and then adds a row with a combo box for each column. The ComboBox just has some string literals in it but when I run the program and load the CSV I get DataGridViewComboBoxColumn { Name=NewBox, Index=-1 }.

Here is the code:

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;
using System.Diagnostics;

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

        public void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            textBox1.Text = openFileDialog1.FileName;

            DataTable table = new DataTable();

            string[] lines = System.IO.File.ReadAllLines(textBox1.Text);
            string firstline = lines[0];
            string[] headerLabels = firstline.Split(',');
            foreach (string headerWord in headerLabels)
            {
                table.Columns.Add(new DataColumn(headerWord));
            }

            int columncount = table.Columns.Count;
            DataGridViewComboBoxColumn dgv = new DataGridViewComboBoxColumn();
            dgv.ReadOnly = false;

            dgv.Name = "NewBox";
            dgv.Items.Add("YES");
            dgv.Items.Add("NO");

            int[] columnnums = new int[columncount];

            DataRow newRow = table.NewRow();

            table.Rows.Add(newRow);

            for (int i = 0; i < columncount; i++)
            {
                newRow[i] = dgv;
            }

            dataGridView1.DataSource = table;
        }
    }
}

Here is a screenshot of the output:

DGV Output


Solution

  • Try like this:

    public void button1_Click(object sender, EventArgs e)
    {
        openFileDialog1.ShowDialog();
        textBox1.Text = openFileDialog1.FileName;
        DataTable table = new DataTable();
    
        string[] lines = System.IO.File.ReadAllLines(textBox1.Text);
        string firstline = lines[0];
        string[] headerLabels = firstline.Split(',');
        foreach (string headerWord in headerLabels)
        {
            table.Columns.Add(new DataColumn(headerWord));
        }
    
        dataGridView1.DataSource = table;
    
        DataGridViewComboBoxColumn dgv = new DataGridViewComboBoxColumn
        {
            ReadOnly = false,
            Name = "NewBox"
        };
    
        dgv.DataSource = new string[] { "YES", "NO" };
        dataGridView1.Columns.Add(dgv);
    }
    

    Update:

    public void button1_Click(object sender, EventArgs e)
    {
        openFileDialog1.ShowDialog();
        textBox1.Text = openFileDialog1.FileName;
    
        string[] lines = System.IO.File.ReadAllLines(textBox1.Text);
        string firstline = lines[0];
        string[] headerLabels = firstline.Split(',');
        foreach (string headerWord in headerLabels)
        {
            DataGridViewComboBoxColumn dgv = new DataGridViewComboBoxColumn
            {
                ReadOnly = false,
                Name = headerWord,
                DataSource = new string[] { "YES", "NO" }
            };
    
            dataGridView1.Columns.Add(dgv);
        }
    }