none
A problem with datagridview combobox RRS feed

  • Question

  • Hi, friends..
    I have a problem relating to datagridview combobox..

    I have a datagridview binded with a customer datatable, one of datagridview columns is combobox which uses student datatable as a datasource (means: datagridViewCombobox.datasource= studentDatatable)..

    Now, when the user adds a new row to the datagridview, and selects a student name from the combobox, everything goes fine, and the new row is added to the customer datatable normally..

    but, when the user wants to add a new name which isn't one of the student names including in combobox list by typing the name manually, the combobox doesn't accept the new entry and the new row isn't added to the datagridview.

    So, how to make the datagridview combobox behaves as a datagridview textbox when the user enters a custom value which doesn't belong the values found in the combobox list. (in other words: how to make datagridview combobox accepts the entering by selecting an item from its list, and in the same time accept the entering manually by the user.)

    I use VS 2010..

    Thank you so much..
    • Moved by CoolDadTx Monday, November 18, 2019 2:47 PM Winforms related
    Monday, November 18, 2019 10:37 AM

All replies

  • Hi SonOfNet,
    You should set the property DropDownStyle to DropDown, which enable typing in the combo box.
    Then use the CellValidating event handler to add the value to the items.
    You can refer to the following code.

    DataGridViewComboBoxColumn comboBoxColumn = new DataGridViewComboBoxColumn();
    private void Form1_Load(object sender, EventArgs e)
    {
        comboBoxColumn.HeaderText = "Student";
        comboBoxColumn.Width = 100;
        comboBoxColumn.Name = "comboBoxColumn";
        dataGridView1.Columns.Add(comboBoxColumn);
    
    }
    private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
    {
    
        if (e.ColumnIndex == comboBoxColumn.DisplayIndex)
        {
            if (!this.comboBoxColumn.Items.Contains(e.FormattedValue))
            {
                this.comboBoxColumn.Items.Add(e.FormattedValue);
            }               
        }
    }
    private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        if (this.dataGridView1.CurrentCellAddress.X == comboBoxColumn.DisplayIndex)
        {
            ComboBox cb = e.Control as ComboBox;
            if (cb != null)
            {
                cb.DropDownStyle = ComboBoxStyle.DropDown;
            }
        }
    }

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 19, 2019 8:48 AM
  •   

    Hi,  Daniel Zhang..

    Thank you so much for your response.. the solution you've given doesn't work if a datagridview combobox's datasource property set to a datatable. So, your solution works fine if the combobox datasource set to null..

    The datagridview combobox datasource in my application set to a student datatable, this means the combobox list, in my application, contains object items, which are datarows, instead of string items.

    So, If I try to use :

    this.comboboxColumn.Items.Contains(e.FormattValue)

    The result is always false, because the C# compiler compares a string value (e.FormattedValue) against objects (Datarows)..

    As well as, the following code line:

    this.comboboxColumn.Items.Add(e.FormattedValue)

    doesn't work if the combobox's datasource set.


    Tuesday, November 19, 2019 4:32 PM
  • Hi SonOfNet,
    You can add the combobox control to the cell. When the new row is selected, it will trigger the SelectionChanged event. And then the combobox control will be added to the new row.
    Here is the code you can refer to.

    private void Form1_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns.Add("name"); dt.Columns.Add("student"); this.dataGridView1.DataSource = dt; } private void dataGridView1_SelectionChanged(object sender, EventArgs e) { ComboBox combo = new ComboBox(); combo.DataSource = new List<string> { "a", "b", "c", "d" }; this.dataGridView1.Controls.Add(combo); combo.Location = this.dataGridView1.GetCellDisplayRectangle(1, dataGridView1.CurrentCell.RowIndex, true).Location; combo.Size = this.dataGridView1.GetCellDisplayRectangle(1, dataGridView1.CurrentCell.RowIndex, true).Size; }

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, November 20, 2019 8:02 AM