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;
} |
this.gridListControl1.KeyDown += GridListControl1_KeyDown;
private void GridListControl1_KeyDown(object sender, KeyEventArgs e)
{
this.gridListControl1.DataSource = dt;
this.gridListControl1.MultiColumn = true;
} |
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;
} |
//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;
}
}
}
} |
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;
}
}
}
}
}
|