Question

HELP! Been trying hard to manually add a DataGridViewRow row WITH a pre-populated combo box... a DataGridViewComboBoxColumn to be exact. I just want to have it automatically populate with strings, shipping options "Ground", "Air", etc. I have gotten so far as to see the first value in the drop down box, but the system says

System.ArguementException: DataGridViewComboBoxCell value is not valid

...and I have never been able to 'drop' the combobox control down (open it up) I want to just make the column, and have it automagically fill with the combobox and its string values, but I have lately been messing around with "DataGridViewComboBoxCell" which I hope I do not ultimately need... THANK YOU I AM STUCK!!!

   DataGridViewComboBoxCell myCbox = new DataGridViewComboBoxCell(); //TEST TBD    
   //
   if ((dgvMasterPrinting.Columns == null) || (dgvMasterPrinting.Columns.Count < 1)) 
   {
   DataGridViewComboBoxColumn dgcShipType = new DataGridViewComboBoxColumn();

   //Column SHIP-TO COMBOBOX                    
   dgcShipType.Name = "colComboShip"; //???
   //dgcShipType.DataPropertyName = "colComboShip"; //TEST TBD
   dgcShipType.HeaderText = "colComboShip";
   dgcShipType.DropDownWidth = 90;
   dgcShipType.Width = 90;
   dgcShipType.MaxDropDownItems = 5;
   dgcShipType.DisplayIndex = 5;
   dgcShipType.FlatStyle = FlatStyle.Flat;
   myCbox.Items.AddRange("A", "N", "P", "S", "Z");
   //   dgcShipType.Items.AddRange("A", "N", "P", "S", "Z");

   //add the other columns (works fine, only the combo box column has problems)
   this.dgvMasterPrinting.Columns.Add(dgcShipType);
   }
   this.dgvMasterPrinting.Rows.Add(...

I made a simple test: using the designer to make a single DatagridviewComboBoxColumn and add items. Funny that this causes an error: 'theDataGridViewComboBoxCell value is not valid' ...I see talk on the www that there is an inherent MS bug lurking within...

Was it helpful?

Solution

NM I figured it out...

                    DataGridViewComboBoxColumn dgcShipType = new DataGridViewComboBoxColumn();
...
 //Column SHIP-TO COMBOBOX                    
                    dgcShipType.ReadOnly = false;
                    dgcShipType.AutoComplete = false;
                    dgcShipType.DisplayStyleForCurrentCellOnly = true;
                    dgcShipType.DropDownWidth = 100;
                    dgcShipType.Width = 110;
                    dgcShipType.HeaderText = "ShipType";
                    dgcShipType.Items.AddRange(new object[] { "GROUND", "LOCAL PATIENT", "3-DAY UPS", "2-DAY SERVICE", "PRIORITY OVERNT", "EARLY AM", "WILL CALL" });
                    dgcShipType.Name = "colComboShip";
                    dgcShipType.MaxDropDownItems = 7;
                    dgcShipType.Resizable = System.Windows.Forms.DataGridViewTriState.True;
                    dgcShipType.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
                    dgcShipType.DefaultCellStyle.NullValue = "GROUND"; //sets the default display value
                    dgcShipType.DisplayIndex = 5;
                    //
...
this.dgvMasterPrinting.Rows.Add(...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top