4

In my datagridview, i have a textboxcolumn and an editable combobox column in winforms.But while typing the new value in combobox text and pressing the enter key, i am not getting the typed value as the corresponding cell value.Could anyone please help with this.

private void dgv_customAttributes_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{

    DataGridViewRow row = dgv_customAttributes.CurrentRow;           
    if (row.Cells[1].Value.ToString() != null)
    {
        //Here the selectedVal is giving the old value instead of the new typed text
        string SelectedVal = row.Cells[1].Value.ToString();
        foreach (CustomAttribute attribute in customAttributes)
        {
            if (row.Cells[0].Value.ToString() == attribute.AttributeName)
            {
                attribute.AttributeValue = SelectedVal;
                break;
            }
        }
    }
}
Leonardo Alves Machado
  • 2,747
  • 10
  • 38
  • 53
Greeshma R
  • 61
  • 1
  • 5
  • 1
    I can't say I know exactly what will happen with what you are doing, but I do know that a bound combobox column must have the same data source for each row. I don't think this is going to work for you. You might use a different mechanism, like popping up a modal pick list maybe. – Crowcoder Sep 10 '18 at 12:48

1 Answers1

1

You need to find out the combo boxes as they are being shown, and attached an event handler to them for when the selected index changes (since it is not possible to obtain that information from the column or the cell themselves).

This means that, unfortunately, capturing the event CellEndEdit is useless.

In the following example, a text box is filled with the chosen option, but you can do anything else, such as selecting a specific value in your enumerated variable or whatever.

    void OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e)
    {
        if ( e.Control is ComboBox comboEdited ) {
            // Can also be set in the column, globally for all combo boxes
            comboEdited.DataSource = ListBoxItems;
            comboEdited.AutoCompleteMode = AutoCompleteMode.Append;
            comboEdited.AutoCompleteSource = AutoCompleteSource.ListItems;

            // Attach event handler
            comboEdited.SelectedValueChanged +=
                (sender, evt) => this.OnComboSelectedValueChanged( sender );
        }

        return;
    }

    void OnComboSelectedValueChanged(object sender)
    {
        string selectedValue;
        ComboBox comboBox = (ComboBox) sender;
        int selectedIndex = comboBox.SelectedIndex;

        if ( selectedIndex >= 0 ) {
            selectedValue = ListBoxItems[ selectedIndex ];
        } else {
            selectedValue = comboBox.Text;
        }

        this.Form.EdSelected.Text = selectedValue;
    }

Find the complete source code for the table in which a column is a combobox in GitHub.

Hope this helps.

Baltasarq
  • 12,014
  • 3
  • 38
  • 57
  • 1
    Thanks for leaving your suggestion.But this will not resolve my problem – Greeshma R Sep 10 '18 at 13:22
  • 1
    Then please edit your question and explain your problem in more detail. – Baltasarq Sep 10 '18 at 13:32
  • I have made my combobox editable also, but I am not able to get the value entered in the text of combobox, so that I can save. – Greeshma R Sep 11 '18 at 05:04
  • I've just learned that in order to get the selected index of this comboboxes, you need to listen to the `SelectedIndexChanged`event. – Baltasarq Sep 11 '18 at 10:07
  • I've updated the answer since the solution is completely elsewhere. – Baltasarq Sep 11 '18 at 13:14
  • I need to get the manually written text .Similar to this question https://stackoverflow.com/questions/33108261/best-way-to-get-selected-item-or-entered-text-from-combobox I have included it on this event OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e) , but it is not working for me. – Greeshma R Sep 12 '18 at 09:34
  • Have updated both the github and the answer. See whether this does the trick. – Baltasarq Sep 12 '18 at 12:59
  • Updated again. You need to capture the selected value changed event, not the selected index changed one. – Baltasarq Sep 13 '18 at 15:28