Search code examples
c#datagridviewdatagridcomboboxcolumn

Is there a way to remove the selection line(highlighted) in a databound combox on a datagridview?


I am having trouble with a databound datagridviewcomboboxcell

I want to remove the blue selection line that appears on a databound comboboxcell.

I noticed that if a comboboxcell is not databound but has a collection of items, the blue line does not appear. however a databound combobox does have it.

item collection

Databound

You will see in the first picture there is no blue selection line however in the next picture(databound comboboxcell) there is...

I need to take this selection line away so that when the databound comboboxcell has only one row of data, a user can through only keyboard inputs can make a selection.

I initially tried to add a keyDown event to set the Items[index] which did change the value, however, when i move off the cell, it displays the Model Name and namespace. Then when going back on to the cell it displays the value.

I used the following code to do this: I added a keydown event to the combobox, and here is the keydown event

private void dataGridView_KeyDown(object sender, KeyEventArgs e)
    {

        if (e.KeyCode == Keys.Enter)
        {
            if (dataGridView1.Columns[dataGridView1.CurrentCell.ColumnIndex].CellType.Name == "DataGridViewComboBoxCell" && dataGridView1.CurrentCell.ReadOnly == false)
            {
                DataGridViewRow row = dataGridView1.CurrentRow;
                try
                {
                    if ((row.Cells[dataGridView1.CurrentCell.ColumnIndex] as DataGridViewComboBoxCell).Items.Count == 1)
                    {
                        (dataGridView1.CurrentRow.Cells[dataGridView1.CurrentCell.ColumnIndex] as DataGridViewComboBoxCell).Value = taxcodes[0];
                        (dataGridView1.CurrentRow.Cells[dataGridView1.CurrentCell.ColumnIndex] as DataGridViewComboBoxCell).DisplayMember = "FullDescription";
                        (dataGridView1.CurrentRow.Cells[dataGridView1.CurrentCell.ColumnIndex] as DataGridViewComboBoxCell).ValueMember = "TaxID";

                    }


                }
                catch
                {

                }
            }
        }

Now that I have attempted to change set the value of the combobox without luck, I am on to the next possible solution i can maybe get to work.

If i can make the Combobox have a no selection line when it opnes initially then as soon as that line moves on to the only item in the list it will select give the ability to select that as the value.

NOTE: a databound comboboxcell with more than one items, works well

NOTE: a non-databound comboboxcell with items defined works well, however i need a displaymember as well as value member

The ideal outcome from this query will give me the ability to select a databound combobox item(making use of the ENTER key) when the combobox has only one item.

Final NOTE: when i use the mouse to make the selection on the databound comboboxcell with ony 1 item, it works perfectly.

Thanks for the help if anyone can help


Solution

  • So after consulting a friend, I managed to override the .ToString() method

    public override string ToString()
    {
         return FullDescription;
    }
    

    This worked for me in this case and the Keydown route was the correct route to go.