1

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

Tm1985
  • 47
  • 1
  • 5

1 Answers1

0

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);
    }
}
L_J
  • 2,351
  • 10
  • 23
  • 28
  • That adds a new column before the first header imported from the CSV. The dropdown works but I need it to insert in the first row under the imported headers. – Tm1985 Jan 07 '20 at 23:20
  • You cannot have rows with different types. If you want the first row to be ComboBoxes than all other rows also will be ComboBoxes. – L_J Jan 07 '20 at 23:30
  • I only want a header row and one row with combo boxes. The data table will never have more than 1 row and a header row. – Tm1985 Jan 07 '20 at 23:36