Avatar of eskie296
eskie296

asked on 

How to make a datagridview column work like a combobox with a "dropDown" DropDownStyle

How can I make a datagridview column work like a combobox with a "dropDown" DropDownStyle instead of "DropDownList" style (i.e. I can either select from list or type in a new entry)?

 I used the following code to create a combobox column in a datagridview, with its dropdown list portion containing sales rep's names in a table called tblSalesReps.  The problem is, no matter which DataGridViewComboBoxDisplayStyle I choose (DropDownButton, ComboBox or Nothing), the combobox only allows me to pick an existing entry from the list, and doesn't allow me to type in a new entry not already in the list.

           
// Using C# in VS2008...
BindingSource bSrc = new BindingSource();
SqlDataAdapter da = new SqlDataAdapter("SELECT SalesRepID, SalesRep FROM tblSalesReps
                                        ORDER BY SalesRep", conn);  // conn is my open connection
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(da);
DataTable table = new DataTable();
da.Fill(table);
bSrc.DataSource = table;
// Create a new combobox column...
DataGridViewComboBoxColumn newColumn = new DataGridViewComboBoxColumn();
newColumn.DataSource = bSrc;      // Get entries for the dropdown list
newColumn.HeaderText = "MySalesRepComboBox";
// Doesn't matter which style I use here...
newColumn.DisplayStyle = DataGridViewComboBoxDisplayStyle.DropDownButton;
newColumn.DisplayMember = "SalesRep";
newColumn.ValueMember = "SalesRepID";
newColumn.DataPropertyName = "SalesRep";
// Add this new column to grdInfo is my datagridview control...
grdInfo.Columns.Insert(5, newColumn);

Thank you.
eskie
C#

Avatar of undefined
Last Comment
eskie296

8/22/2022 - Mon