简体   繁体   中英

C# Datagridview ComboBox Select Value Stick

I want to be able to display the selected intem in a combobox in the datagridview. I already have the combobox populated and I already know how to get the selected value from a database. What I am trying to accomplish is anytime the form is loaded any pre-selected values in the combobox are automatically pre-selected to show the user what they selected last. I have a database that stores the selections and they are easily accessed. Again, how can I make the selections stick to whatever a user selects so they are remembered each and everytime they open the form, unless of course someone changes their selection and its saved. I don't see a value.selected in the datagridview combobox like you normally do. Has anyone ever gotten this to work?

Here is an example:

I have a combobox imbedded in a datagridiew called test as the column name

there are 4 fields to select:

  1. test1
  2. test2
  3. test3
  4. test4

Lets say this time I select test3 as my choice. I close the form and the selection is saved to a database. Now I know how to retrieve that value from the database, but how do I make test 3 show when the form loads as the displaymember and still be able to select another if I choose to?

Handle the Cell Validating event as follows...

void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
    if (cell is DataGridViewComboBoxCell)
    {        
        DataGridViewComboBoxCell cell = DataGridViewComboBoxCell)dataGridView1.CurrentCell;
        cell.Items.Clear();
        cell.Items.Add(e.FormattedValue);
        dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
        cell.Value = e.FormattedValue;
    }
}

basically you have to add the selected value to the the items collection of the cell and then set the value = to the added value. If the value is not in the items collection, you will get a DataError event on the cell.

the cell items collection is NOT the same as the ComboBox Items collection.

That is how to handle setting the value of the cell based on the selection.

If you handle the EditingControlShowing Event of the datagridview, you can get access to the combobox control when it is clicked, and set the selected item to the current value of the cell before it is drawn for the user.

void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        if (dataGridView1.CurrentCell is DataGridViewComboBoxCell
            && e.Control is DataGridViewComboBoxEditingControl)
        {
            DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)dataGridView1.CurrentCell;
            ComboBox ctrl = (ComboBox)e.Control;
            // Get Currently selected value...
            string curValue = String.Empty;
            if (cell.Value != null)
                curValue = cell.Value.ToString();

            //bind data
            ctrl.DataSource = dataBaseData;
            //set selected value
            ctrl.SelectedItem = curValue;
        }

    }

I believe all you need to do is set .Value on the cell and it will automatically set the selected item. You could loop over all the cells and set them to their previously selected values.

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