allow typed value OR autocomplete

I'm developing a WinForms app in C# / VS2010. In two places I've a requirement to bind a ComboBox to data table row items (lets call the table 'updateTable'). Users can input any text here. However, to assist users, the ComboBox list is populated from a different table (lets call it 'lookupTable') with SuggestAppend offering values that might be used.

I have this working perfectly in a free standing combo box by setting the following values through the form designer:

  • Databindings = updateTable fieldname
  • Datasource = lookupTableBindingSource
  • DisplayMember = lookupTable fieldname
  • ValueMember =
  • AutocompleteMode = SuggestAppend
  • AutoCompleteSource = ListItems
  • AutoCompleteCustomSource = (Collection)
  • SelectedItem = (none)
  • SelectedValue = (none)
  • I also need to achieve this in a similar way through in a ComboBox on a DataGridView and can't get this to work. What I've tried is:

    Setting up these properties through the form designer:

  • DataPropertyName = updateTable fieldname
  • DataSource = lookupTableBindingSource
  • DisplayMember = lookupTable fieldname
  • Items (Collection)
  • ValueMember = (none)
  • Through code I'm also adding:

        private void identitiesDataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (e.Control is DataGridViewComboBoxEditingControl)
            {
                ComboBox combo = (ComboBox)e.Control;
                ((ComboBox)e.Control).DropDownStyle = ComboBoxStyle.DropDown;
                ((ComboBox)e.Control).AutoCompleteSource = AutoCompleteSource.ListItems;
                ((ComboBox)e.Control).AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
                combo.Validated -= new EventHandler(combo_Validated);
            }
        }
    
        void combo_Validated(object sender, EventArgs e)
        {
            Object selectedItem = ((ComboBox)sender).SelectedItem;
            DataGridViewComboBoxColumn col = (DataGridViewComboBoxColumn)identitiesDataGridView.Columns[identitiesDataGridView.CurrentCell.ColumnIndex];
            if (!String.IsNullOrEmpty(col.ValueMember))
                identitiesDataGridView.CurrentCell.Value = GetPropValue(selectedItem, col.ValueMember);
            else
                identitiesDataGridView.CurrentCell.Value = selectedItem;
        }
    
        public static object GetPropValue(object src, string propName)
        {
           if (src == null)
               return null;
           return src.GetType().GetProperty(propName).GetValue(src, null);
        }
    
        private void identitiesDataGridView_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (identitiesDataGridView.IsCurrentCellDirty)
            {
                identitiesDataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
        }
    

    This is a) giving me errors when the form is launched because some updateTable values are not found in the lookupTable and b) not allowing me to select anything other than lookupTable values when I edit.

    Any idea how I can achieve this "type what you like but here are some suggestions" effect in a datagrid?

    链接地址: http://www.djcxy.com/p/60906.html

    上一篇: 当用户将文本输入到combox中时如何禁用combox下拉菜单

    下一篇: 允许输入值或自动完成