How to add a row to a DataGridView that has a ComboBox

Marc Graham 26 Reputation points
2024-03-16T19:15:22.5966667+00:00

I've been running this application for years. I just went from w10 to w11 and get the error message "DataGridViewComboBoxCell value is not valid" when loading data.

The DataGridView has a ComboBox column, but I add structures that have only strings. Previously, the string values would be added to the ComboBox as appropriate and all the rows shared that ComboBox (or, at least, they shared the same list).

Is there documentation somewhere how to load data into a DGV ComboBox column?

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Testing2
{
    internal enum colHdrInfo { Payee, Type, Date, Amount, Source, Comment }
    public partial class Form1 : Form
    {
        private DataGridView dgv = new DataGridView();
        private string titleFormat = @"Accounts from {0:d} to {1:d}";
        public Form1()
        {
            InitializeComponent();
            finishLayout();
        }
        private readonly int[] colWidths = new int[] { 50, -1, -1, 10 };
        private String[] allowedTypes;
        private void finishLayout()
        {
            createDataGridView();
            IsMdiContainer = true;
            Text = "Accounts";
        }

        private void createDataGridView()
        {

            dgv.RowHeaderMouseClick += Dgv_RowHeaderMouseClick;
            dgv.DataError += Dgv_DataError;
            dgv.BackgroundColor = Color.White;
            Font oldDefault = dgv.ColumnHeadersDefaultCellStyle.Font;
            dgv.ColumnHeadersDefaultCellStyle.Font = new Font(oldDefault, FontStyle.Bold);
            dgv.Dock = DockStyle.Fill;
            dgv.Name = "My CheckBook";
            DataGridViewColumn dvgCol;
            foreach (colHdrInfo cHI in Enum.GetValues(typeof(colHdrInfo)))
            {
                dvgCol = !cHI.Equals(colHdrInfo.Type) ? createTextBox(cHI) : createComboBox(cHI);
                //dvgCol = createTextBox(cHI);
                dgv.Columns.Add(dvgCol);
            }
            dgv.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
            dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            this.Controls.Add(dgv);
        }
        private void Dgv_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            throw new NotImplementedException();
        }
        // deletes row on right click on header.
        private void Dgv_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            String fileName = "C:\\Users\\Marc\\OneDrive\\Documents\\Financial\\Checkbook\\test.csv";
            if (e.Button != MouseButtons.Right) return;
            populateGridView(fileName);

        }
        private AutoCompleteStringCollection _acsc = new AutoCompleteStringCollection();
        private ComboBox _cbo; // this is a nasty trick to pass the combobox from one event handler to another
        private DataGridViewColumn createComboBox(colHdrInfo cHI)
        {
            allowedTypes = new string[] { "ABC,DEF,GHI,JKL,MNO,PQR" };
            Array.Sort(allowedTypes);
            // _acsc.AddRange(allowedTypes);
            DataGridViewColumn blah = new DataGridViewComboBoxColumn()
            {
                HeaderText = cHI.ToString(),
                Name = cHI.ToString(),
                DataSource = allowedTypes,
                DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing,
                SortMode = DataGridViewColumnSortMode.Programmatic
            };
            return blah;
        }
        private DataGridViewColumn createTextBox(colHdrInfo cHI)
        {
            DataGridViewColumn blah = new DataGridViewTextBoxColumn();
            return blah;
        }
        internal void populateGridView(String fileName)
        {
            StreamReader sr;
            try
            {
                sr = File.OpenText(fileName);
            }
            catch (System.IO.IOException e)
            {
                MessageBox.Show($"IO Error Reading {fileName}: {e.GetType().Name}", "Error", MessageBoxButtons.OK);
                return;
            }
            string line;
            // collect earliest and latest time values
            DateTime early = DateTime.MaxValue, late = DateTime.MinValue;
            while ((line = sr.ReadLine()) != null)
            {
                string[] vv = line.Split(',');
                if (vv.Length > Enum.GetValues(typeof(colHdrInfo)).Length)
                    if (MessageBox.Show($"this line {line} is badly formed\nProceed?", "Bad Input", MessageBoxButtons.OKCancel) == DialogResult.Cancel) Application.Exit();
                try
                {
                    DataGridViewRow yydgv = new DataGridViewRow();
                    yydgv.CreateCells(dgv, vv);
                    dgv.Rows.Add(yydgv);
                }
                catch (Exception exp)
                {
                    if (!_acsc.Contains(vv[(int)colHdrInfo.Type]))
                        if (MessageBox.Show($@"Bad Classification {vv[1]}\nDo you care?", "oh yeah", MessageBoxButtons.YesNo) == DialogResult.No) continue;// not quite rright
                        else throw exp;
                }

                // end of reading text file
                sr.Close();

            }
        }
    }
}
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,873 questions
Windows 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
11,195 questions
Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
8,988 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. Jiale Xue - MSFT 43,046 Reputation points Microsoft Vendor
    2024-03-19T10:01:45.28+00:00

    Hi @Marc Graham , Welcome to Microsoft Q&A,

    When loading data into a ComboBox column of a DataGridView (DGV), you need to ensure the following:

    Data Binding: Make sure your ComboBox columns are bound to a data source. This means that you must specify a data source that contains the items you want to display in the ComboBox. This is usually a list or data table.

    ComboBox Formatting: For each item you want to display in the ComboBox, you need to specify how the text is displayed. Typically, you would set properties that display text and values. For example, in Windows Forms, you might use the DisplayMember and ValueMember properties.

    Adding data: Before loading data into the DataGridView, make sure you have added the required data to the ComboBox's data source.

    Data loading timing: Ensure that the ComboBox column settings and data source binding have been completed before loading data into the DataGridView. This way, when loading data, the ComboBox column will correctly display and select the corresponding item.

    // Assume your DataGridView control is named dataGridView1
    // Assume your ComboBox column is on column index 0
    
    //Set the data source and formatting of the ComboBox column
    var comboBoxColumn = (DataGridViewComboBoxColumn)dataGridView1.Columns[0];
    comboBoxColumn.DataSource = YourDataSource; // Set the data source of the ComboBox column
    comboBoxColumn.DisplayMember = "DisplayPropertyName"; // Displayed property name
    comboBoxColumn.ValueMember = "ValuePropertyName"; //The property name of the value
    

    If you use a combo box, you may need to analyze it in detail. Provide a reproducible use case.

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.