How to fill DataGridViewComboBoxColumn items differently for each row

I have the following code:

        private void FillJobDescriptionItems(int rowIndex)
        {
            DataGridViewRow row = dataGridViewTimesheetEntries.Rows[rowIndex];

            // Get the job number and title, and customer name for a specific job
            Job? jobDescriptionObj = (Job?)row.Cells[jobDataGridViewTextBoxColumn.Index].Value;
            if (jobDescriptionObj != null)
            {
                VwJob? jobVwJob = (VwJob?)_dbContext.VwJobs.Where(x => x.Id == jobDescriptionObj.Id).FirstOrDefault();
                if (jobVwJob != null)
                {
                    List<VwJob> jobCustomers = (List<VwJob>)(_dbContext.VwJobs.Where(x => x.CustomerId == jobVwJob.CustomerId).ToList());
                    DataGridViewComboBoxCell jobDescriptionCell = (DataGridViewComboBoxCell)row.Cells[JobDescription.Index];
                    jobDescriptionCell.Items.Clear();
                    jobDescriptionCell.ValueMember = "Id";
                    jobDescriptionCell.DisplayMember = "Name";
                    foreach (VwJob jobcust in jobCustomers)
                    {
                        CustomerJobStatus custJobStatusObj = new CustomerJobStatus();
                        custJobStatusObj.Id = jobcust.Id;
                        custJobStatusObj.Name = jobcust.JobNumber + " | " + jobcust.CustomerName + " | " + jobcust.Title;

                        jobDescriptionCell.Items.Add(custJobStatusObj);
                    }
                }
                else
                {
                    row.Cells[JobDescription.Index].Value = jobDescriptionObj.JobNumber + " |  | " + jobDescriptionObj.Title;
                }
            }
        }

Which is being called in the DataBindingComplete event. The DataGridView is coming back empty for this column.

Please can someone tell me how to fill each DataGridViewComboBoxCell with the items for just one customer?

Thanks.

I’ve tried using Items.Add and also DataSource as well, but with no luck.

Leave a Comment