We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Showing the items in dropdown

Hi,
Can we see the items of a gridlistcontrol in a dropdown with the enter key event handler?

I mean when we press the enter key the gridlistcontrol should show all the items in the dropdown.

Please suggest.


7 Replies

AA Arulraj A Syncfusion Team January 4, 2019 05:00 AM UTC

Hi RajeshKumar, 

Thanks for using Syncfusion product. 

To show the items in GridListControl dropdown, you could use the CurrentCell.ShowDropDown method in CurrentCellKeyUp event. Please refer the following code example and the sample. 

GridControl 
this.gridControl1.CurrentCellKeyUp += GridControl1_CurrentCellKeyUp; 
private void GridControl1_CurrentCellKeyUp(object sender, KeyEventArgs e) 
{ 
    if (e.KeyData == Keys.Enter) 
    { 
        GridCurrentCell currentCell = this.gridControl1.CurrentCell; 
        GridStyleInfo style = this.gridControl1[currentCell.RowIndex, currentCell.ColIndex]; 
        if (style.CellType.Equals(GridCellTypeName.GridListControl)) 
        { 
            if (!isDropedDownClosed) 
            { 
                currentCell.ShowDropDown(); 
            } 
            else 
                isDropedDownClosed = false; 
        } 
    } 
} 
//To handle the enter key to select the item in dropdown using enter key 
this.gridControl1.CurrentCellCloseDropDown += GridControl1_CurrentCellCloseDropDown; 
bool isDropedDownClosed; 
private void GridControl1_CurrentCellCloseDropDown(object sender, Syncfusion.Windows.Forms.PopupClosedEventArgs e) 
{ 
    isDropedDownClosed = true;    
} 
 
 
GridListControl 
this.gridListControl1.KeyDown += GridListControl1_KeyDown; 
private void GridListControl1_KeyDown(object sender, KeyEventArgs e) 
{ 
    this.gridListControl1.DataSource = dt; 
    this.gridListControl1.MultiColumn = true; 
} 
 
GridGroupingControl 
this.gridGroupingControl1.TableControl.CurrentCellKeyUp += TableControl_CurrentCellKeyUp; 
private void TableControl_CurrentCellKeyUp(object sender, KeyEventArgs e) 
{ 
    if (e.KeyData == Keys.Enter) 
    { 
        GridCurrentCell currentCell = this.gridGroupingControl1.TableControl.CurrentCell; 
        GridStyleInfo style = this.gridGroupingControl1.TableModel[currentCell.RowIndex, currentCell.ColIndex]; 
        if (style.CellType.Equals(GridCellTypeName.GridListControl)) 
        { 
            if (!isDropedDownClosed) 
            { 
                currentCell.ShowDropDown(); 
            } 
            else 
                isDropedDownClosed = false; 
        } 
    } 
} 
 
this.gridGroupingControl1.TableControl.CurrentCellCloseDropDown += TableControl_CurrentCellCloseDropDown; 
bool isDropedDownClosed; 
private void TableControl_CurrentCellCloseDropDown(object sender, PopupClosedEventArgs e) 
{ 
    isDropedDownClosed = true; 
} 
 
 
Please let us know if you need any further assistance on this. 

Arulraj A 



RS RajeshKumar Swain January 6, 2019 05:13 AM UTC

Thanks.

I used the below code and got the dropdown in enter key now.
if (Keys.Enter == e.Keycode)
{
this.CurrentCell.ShowDropDown();
e.Handled = true;
}

Now I wanted to navigate the list item using up/down arrow key. 
                                this.ListControl.Focus();
                                this.ListControl.SelectedIndex = 0;
                               
                                if (Keys.Up == e.KeyCode)
                                {
                                    if(this.ListControl.SelectedIndex == 0)
                                    {
                                        this.ListControl.SelectedIndex = this.ListControl.Items.Count - 1;
                                    }
                                    else
                                    {
                                        this.ListControl.SelectedIndex = this.ListControl.SelectedIndex - 1;
                                    }
                                }

I am able to select the last the last item in above case.
I wanted the dropdown to be open and I can further make the use of upkey. But the dropdown is closing down.

please suggest.

regards


AA Arulraj A Syncfusion Team January 7, 2019 08:42 AM UTC

Hi RajeshKumar, 
 
Thanks for your update. 
 
We have tested your reported scenario in our GridControl and GridGroupingControl using our latest Syncfusion product version 16.4.0.42. But, the sample is working fine as we expected. GridListControl dropdown is not getting closed when press the up/down arrow from first/last item. We suspect that this issue may occur based on your customization. So, please let us know your all grid and GridListControl related customization or let us know the below attached sample differ from your customization. It will be helpful for us to provide the solution at the earliest. 
 
 
Arulraj A 



RS RajeshKumar Swain January 8, 2019 11:52 AM UTC

Hi,
I am trying to make things clear. I am using only GridListControl.   I checked the sample you have attached before is not using GridlistControl enter key handling. I put the break point but it is not going to private void GridListControl1_KeyDown(object sender, KeyEventArgs e)

Now I am putting my sample code.
namespace Syncfusion.Windows.Forms.Grid
{
    public class GridDropDownGridListControlCellRenderer : GridDropDownCellRenderer
    {
          public GridListControl ListControlPart { get; }
          
…….. // Other things
}

in other file 
public class xyz : GridDropDownGridListControlCellRenderer
  {

     protected override void OnKeyDown( KeyEventArgs e )
        {
            GridStyleInfo s = this.StyleInfo;
                   if( !e.Handled )
                    {
                        switch( e.KeyCode )
                        {
                              case Keys.Enter:
                                   // I want the drop down to open here but it is not happening
                              case Keys.Up:
                                  // I want to go up the item list one step
                               case Keys.Down:
                                   // I want to go down the item list one step



So please suggest


AA Arulraj A Syncfusion Team January 9, 2019 05:39 AM UTC

Hi RajeshKumar, 

Thanks for your update. 

In your code part, you have implemented the custom cell renderer for GridDropDownGridListControlCellrenderer for GridListCotnrol. To achieve your requirement, you could handle the KeyDown event using Handled property in OnKeyDown method. Please refer the following code example and the sample. 

C# 
//Cell renderer initialization for GridListControl 
this.gridControl1.CellRenderers[GridCellTypeName.GridListControl] = new GridDropDownGridList 
 
public class GridDropDownGridListControlCellRendererAdv : GridDropDownGridListControlCellRenderer 
{ 
    public GridDropDownGridListControlCellRendererAdv(GridControlBase grid, GridCellModelBase model) : base(grid, model) 
    {   } 
 
    protected override void OnKeyDown(KeyEventArgs e) 
    { 
        GridStyleInfo s = this.StyleInfo; 
        if (!e.Handled) 
        { 
            switch (e.KeyCode) 
            { 
                case Keys.Enter: 
                    this.CurrentCell.ShowDropDown(); 
                    e.Handled = true; 
                    break; 
                case Keys.Up: 
                    if (this.ListControlPart.SelectedIndex == 0) 
                        this.ListControlPart.SelectedIndex = this.ListControlPart.Items.Count - 1; 
                    else 
                        this.ListControlPart.SelectedIndex -= 1; 
                    e.Handled = true; 
                    break; 
                case Keys.Down: 
                    if (this.ListControlPart.SelectedIndex == this.ListControlPart.Items.Count - 1) 
                        this.ListControlPart.SelectedIndex = 0; 
                    else 
                        this.ListControlPart.SelectedIndex += 1; 
                    e.Handled = true; 
                    break; 
            } 
        } 
    } 
} 
 
 
Arulraj A 



RS RajeshKumar Swain January 15, 2019 02:02 PM UTC

Thanks for your follow up Arulraj,

Your code working with some proble
1. With Enter key dropdown is opening ---> working as destined
2. Using Arrow up/down key able to select the items  ---> working as desired
3. When the enter is pressed again it is not taking the item selected in 2. ---> not working as desired

I have the below code with explanation plz have a look.
[code]

namespace Syncfusion.Windows.Forms.Grid
{
    public class GridDropDownGridListControlCellRenderer : GridDropDownCellRenderer
    {
          public GridListControl ListControlPart { get; }
          
…….. // Other things
}

in other file 

public class xyz : GridDropDownGridListControlCellRenderer
  {

     protected override void OnKeyDown( KeyEventArgs e )
        {
            GridStyleInfo s = this.StyleInfo;
             if( s.CellModel is xyzGDBGridListControlCellModel && s.Tag is xyzComboBoxGUIData )
            {
                 xyzComboBoxGUIData guiData = s.Tag as xyzComboBoxGUIData;
                if( guiData.AccessRight != ACCESS_RIGHT.READONLY )
                {
                   if( !e.Handled )
                    {
                        switch( e.KeyCode )
                        {
                              case Keys.Enter:
                                   MessageBox.Show("Enter key " + this.CurrentCell.IsDroppedDown);// + "--------" + "Arrow key " + this.arrowKeyPressed + " === " + idx + "index " + this.ListControlPart.SelectedIndex);
                               
                                if (!this.CurrentCell.IsDroppedDown)
                                {
                                    this.CurrentCell.ShowDropDown();
                                    if (this.ListControlPart.Items.Count > 1)
                                        this.ListControlPart.SelectedIndex = -1;
                                                                                                        
                                        e.Handled = true;
                                }
                                                               
                                break;
                              case Keys.Up:
                              case Keys.Down:
                                 
                              break;
               }
                    }
                }
            }
           
                base.OnKeyDown(e);   // IT IS CRASHING HERE FOR LIST WITH ONLY ONE ITEM. IT IS NOT CRASHING FOR LIST CONTROL WITH MORETHAN 1 ITEM.
        }

[/code]
it is crashing showing below error 
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index at System.Collection.ArrayList.get_item(int32 index) at Syncfusion.Windows.Forms.Grid.GridDropDownGridListControlCellRenderer.OnKeyDown(KeyEventArgs e)

Please check if the problem is there in the dll as it is working fine for Gridlistcontrol with more than items and failing for Gridlistcontrol with 1 item.


AA Arulraj A Syncfusion Team January 17, 2019 09:45 AM UTC

Hi RajeshKumar, 

Thanks for your update. 

Query 
Response 
3. When the enter is pressed again it is not taking the item selected in 
 2. not working as desired 
 
To load the selected value in GridListControl, you could use the SetControlText method in OnKeyDown method. Please refer the following code example. 
 
C# 
case Keys.Enter: 
    if (!this.CurrentCell.IsDroppedDown) 
    { 
        this.CurrentCell.ShowDropDown(); 
        if (this.ListControlPart.Items.Count > 1) 
            this.ListControlPart.SelectedIndex = -1; 
 
        e.Handled = true; 
    } 
    else 
    { 
        this.CurrentCell.CloseDropDown(Syncfusion.Windows.Forms.PopupCloseType.Done); 
        if (this.ListControlPart.SelectedIndex >= 0) 
        { 
            object item = ListControlPart.Items[this.ListControlPart.SelectedIndex]; 
            ControlValue = ListControlPart.GetItemValue(item); 
        } 
        string controlText = s.GetFormattedText(ControlValue, GridCellBaseTextInfo.CurrentText); 
        this.OnSetControlText(controlText); 
    } 
    break; 
it is crashing showing below error  
ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.

 
We have handled this exception in our latest Syncfusion product version 16.4.0.42. So, you can upgrade to our latest Syncfusion product version to resolve your reported scenario or you could resolve this issue by implementing the below code in OnKeyDown method. Please refer the following code example. 

C# 
protected override void OnKeyDown(KeyEventArgs e) 
    if (!e.Handled) 
        { 
        } 
        else if (IsDroppedDown && Control.ModifierKeys == Keys.None) 
    { 
        int curSel = this.ListControlPart.SelectedIndex; 
        int linesVisible = this.ListControlPart.Items != null && this.ListControlPart.Items.Count > 0 ? (this.ListControlPart.Height / this.ListControlPart.GetItemHeight(0)) - 1 : 0; 
        if (this.IsEditing && this.StyleInfo.DropDownStyle == GridDropDownStyle.Editable) 
        { 
            if (this.StyleInfo.AutoCompleteInEditMode == GridComboSelectionOptions.Both) 
            { 
                if (!string.IsNullOrEmpty(TextBoxText)) 
                { 
                    curSel = FindItem(TextBoxText, true, -1, true); 
 
                    if (curSel > -1) 
                        TextBoxText = this.ListControlPart.Items[curSel].ToString(); 
                    TextBox.SelectAll(); 
                    e.Handled = true; 
                } 
            } 
        } 
    } 
 
 


If you are still facing the issue, please provide the Syncfusion product version details which you have used in your application or let us know the above attached sample differ from your customization. It will be helpful us address the issue at the earliest. 
 
Arulraj A 


Loader.
Live Chat Icon For mobile
Up arrow icon