简体   繁体   中英

Can I have a DataGridView cell where I can type OR choose from a drop-down list?

This is my first time using the DataGridView, and it's a bit overwhelming (so many options!).

I'd like to display a list of properties, one per row. Each row is a name/value pair. The names of the properties are fixed, but the user is free to enter any value.

Now, since each property has a list of already-used values , I'd like those to be available to the user in a drop-down list . However, I'd also like the user to be able to type a new value . Ideally, the value should auto-complete as the user types.

I've tried using a column with the DataGridViewComboBoxColumn style, and since a standard combo box supports the type-or-edit way of working, I though that this would work. However, it seems to only allow selecting from the list (plus, pressing a key will automatically select the first matching entry from the list). There seems to be no way to type a new value.

Which of the 844 properties should I set? :-)

You have to change the combobox DropDownStyle to allow for the user to enter text while the combobox is in edit mode, but the standard DataGridView does not allow this behavior at Design-Time so you have to pull some tricks, handle CellValidating, EditingControlShowing and CellValidated event.

Here's the code (from MSDN forum, it works for me).

         private Object newCellValue;
         private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
         {
             if (dataGridView1.CurrentCell.IsInEditMode)
             {
                 if (dataGridView1.CurrentCell.GetType() ==
                 typeof(DataGridViewComboBoxCell))
                 {
                     DataGridViewComboBoxCell cell =
                     (DataGridViewComboBoxCell)dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];

                     if (!cell.Items.Contains(e.FormattedValue))
                     {
                         cell.Items.Add(e.FormattedValue);

                         cell.Value = e.FormattedValue;

                         //keep the new value in the member variable newCellValue
                         newCellValue = e.FormattedValue;
                     }
                 }
             }
         }

         private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
         {
             if (e.Control.GetType() ==
typeof(DataGridViewComboBoxEditingControl))
             {
                 ((ComboBox)e.Control).DropDownStyle = ComboBoxStyle.DropDown;
             }
         }

         private void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e)
         {
             if (dataGridView1.CurrentCell.GetType() ==
                 typeof(DataGridViewComboBoxCell))
             {
                 DataGridViewCell cell =
                 dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                 cell.Value = newCellValue;
             }
         }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM