On runtime I am changing some columns of datagridview into combobox columns. Now how do I get the existing distinct values in the combobox items? I am using entity model as datasource. My code is:
dgvLoadTable.DataSource = null; var context = new AdminEntities(); var TableName = cboSelectTable.Text.ToString(); var rawData = context.GetType().GetProperty(TableName).GetValue(context, null); var truncatedData = ((IQueryable<object>)rawData).Take(0); var source = new BindingSource { DataSource = truncatedData }; dgvLoadTable.DataSource = source; dgvLoadTable.ReadOnly = false; dgvLoadTable.AllowUserToAddRows = true; for (int row= 0; row < dgvLoadTable.Rows.Count; row++) { for (int col = 0; col < dgvLoadTable.Columns.Count; col++) { if (col == 2 || col == 4) { this.dgvLoadTable[col, row] = new DataGridViewComboBoxCell(); var item = dgvLoadTable.Rows.Cast<DataGridViewRow>().Where(r => r.Cells[0].Value != null).Select(r => r.Cells[col].Value).Distinct(); ((DataGridViewComboBoxCell)dgvLoadTable[col, row]).Items.AddRange(item.ToArray()); dgvLoadTable[col, row].ValueType = typeof(var); } } } dgvLoadTable.Refresh();
But this is not working, any suggestion?