Stephen Anderson

Stephen Anderson

  • NA
  • 16
  • 2.7k

Populate datagridview column combobox datasource

Jun 13 2018 1:46 PM
Hello:
 
I would like the correct C# syntax to populate datagridview column comboboxes. Each column will look at a query (Entity Framework 6), converted to a list, and would make those values the correct ones to choose from. There should also be a blank for a default.
 
Currently, I have this code.
  1. DataTable dt_Employee = timeDataSet.Tables["Employee"];  
  2. DataTable dt_TimeData = timeDataSet.Tables["TimeData"];  
  3. DataTable dt_Activity = timeDataSet.Tables["Activity"];  
  4. DataTable dt_Jobs = timeDataSet.Tables["Jobs"];  
  5. var qryProjectNumbers = from a in dt_Jobs.AsEnumerable()  
  6. orderby a.Field<String>("ProjectNumber")  
  7. where (a.Field<String>("ProjectNumber") != null)  
  8. select a.Field<String>("ProjectNumber");  
  9. var lstProjectNumbers = qryProjectNumbers.Distinct().ToList();  
  10. var qryActivity = from a in dt_Jobs.AsEnumerable()  
  11. orderby a.Field<String>("Activity")  
  12. where (a.Field<String>("Activity") != null)  
  13. select a.Field<String>("Activity");  
  14. var lstActivity = qryActivity.Distinct().ToList();  
  15. // How do I add combobox columns?  
  16. dgvTime.Controls.OfType<DataGridViewComboBoxColumn>().ElementAt(iProjectNo).DataSource = lstProjectNumbers;  
  17. dgvTime.Controls.OfType<DataGridViewComboBoxColumn>().ElementAt(iActivity).DataSource = lstActivity;  
  18. this.timeDataTableAdapter1.Fill(this.timeDataSet.TimeData);  
Thank you.

Answers (4)