Category / Section
How to display the selected item at the top of the dropdown?
1 min read
In order to display the highlighted choice at top of the drop down, CurrentCellKeyPress event and TextChanged event of GridComboBoxCellRenderer’s TextBox can be used. The top row index of the drop down list as the selected index can be set in the TextChanged event.
Code Snippet
C#
//Triggering the CurrentCellKeyPress event. this.gridDataBoundGrid1.CurrentCellKeyPress += new KeyPressEventHandler(gridDataBoundGrid1_CurrentCellKeyPress); private GridComboBoxCellRenderer renderer; //Handling the CurrentCellKeyPress event. private void gridDataBoundGrid1_CurrentCellKeyPress(object sender, KeyPressEventArgs e) { renderer = this.gridDataBoundGrid1.Model.CurrentCellRenderer as GridComboBoxCellRenderer; if (renderer != null) { renderer.TextBox.TextChanged += new System.EventHandler(TextBox_TextChanged); } } private void TextBox_TextChanged(object sender, EventArgs e) { if (this.gridDataBoundGrid1.CurrentCell.IsDroppedDown && renderer.ListBoxPart.SelectedItem != null) { //Move the selected value to the top of the drop down renderer.ListBoxPart.TopIndex = renderer.ListBoxPart.SelectedIndex; } }
VB
' Triggering the CurrentCellKeyPress event. AddHandler Me.gridDataBoundGrid1.CurrentCellKeyPress, AddressOfgridDataBoundGrid1_CurrentCellKeyPress Private renderer As GridComboBoxCellRenderer ' Handling the CurrentCellKeyPress event. Private Sub gridDataBoundGrid1_CurrentCellKeyPress(ByVal sender As Object, ByVal e AsKeyPressEventArgs) renderer = TryCast(Me.gridDataBoundGrid1.Model.CurrentCellRenderer,GridComboBoxCellRenderer) If renderer IsNot Nothing Then AddHandler renderer.TextBox.TextChanged, AddressOf TextBox_TextChanged End If End SubPrivate Sub TextBox_TextChanged(ByVal sender As Object, ByVal e As EventArgs) If Me.gridDataBoundGrid1.CurrentCell.IsDroppedDown AndAlsorenderer.ListBoxPart.SelectedItem IsNot Nothing Then 'Move the selected value to the top of the drop down renderer.ListBoxPart.TopIndex = renderer.ListBoxPart.SelectedIndex; End IfEnd Sub
Sample Links