I have a DataGridView with a Column already defined as ComboBox. How do I add items to that Column Combobox so that they are available in that column for each new row?
I have tried everything I could think of or find on the web. Here's the latest attempt:
[![ static DataGridViewComboBoxColumn dgvCmb = new DataGridViewComboBoxColumn(); static DataGridViewComboBoxCell dgvCmbCell = new DataGridViewComboBoxCell(); DataTable dt = new DataTable();
private void frmReporting_Load(object sender, EventArgs e)
{
dt = Populate("SELECT DISTINCT OperatorName FROM tblOperator WHERE OperatorName NOT LIKE '*%' AND Active='1' ORDER BY OperatorName");
dgvCmbCell = FillComboBox();
dgvReports[1, 0] = dgvCmbCell;
}
private DataGridViewComboBoxCell FillComboBox()
{
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
//DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
DataGridViewComboBoxCell combo = new DataGridViewComboBoxCell();
ArrayList row = new ArrayList();
foreach (DataRow dr in dt.Rows)
{
row.Add(dr[0].ToString());
}
combo.Items.AddRange(row.ToArray());
return combo;
}
private DataTable Populate(string sqlCommand)
{
myConnection.Open();
SqlCommand command = new SqlCommand(sqlCommand, myConnection);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = command;
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
adapter.Fill(table);
myConnection.Close();
return table;
}