C# DevExpress按回车键自动跳转单元格或新增记录(GridMovetor.cs)
GridView绑定KeyDown事件
C# 全选
gvDetail.KeyDown += gvDetail_KeyDown;
KeyDown事件
C# 全选
//给明细表绑定KeyDown事件,按回车自动跳格
private void gvDetail_KeyDown(object sender, KeyEventArgs e)
{
//表格按回车键自动跳转到下一个可编辑列, 若是最后一列,自动新增一条记录
GridMovetor.OnGridViewKeyDown(gvDetail, e);
}
GridMovetor 类
C# 全选
/// <summary>
/// GridView移动表格单元管理类, 最后版本:2023-11-13
/// </summary>
public class GridMovetor
{
/// <summary>
/// 处理表格KeyDown事件。
/// <para>1.按回车或Tab键自动跳到下一个栏位.</para>
/// <para>2.处理在最后一条记录的最后一栏按回车键新增一条记录.</para>
/// </summary>
public static void OnGridViewKeyDown(object sender, KeyEventArgs e)
{
if (!(e.KeyCode == Keys.Enter || e.KeyCode == Keys.Tab)) return;
if (sender is GridView)
{
GridView view = (GridView)sender;
GridControl grid = view.GridControl;
bool isLastCol = IsLastEditableColumn(view, view.FocusedColumn);
bool isLastRow = view.FocusedRowHandle == view.RowCount - 1;
//不是最后可编辑列, 则移至下一列
if (isLastCol == false || isLastRow == false)
{
MoveNextColumnOrRow(view, view.FocusedColumn, new KeyPressEventArgs((char)Keys.Enter));
e.Handled = true;
e.SuppressKeyPress = true;
return;//任务完成,不处理以下任何事务
}
//最后一条记录的最后一列,自动添加记录
if (isLastRow && isLastCol && view.OptionsBehavior.Editable)
{
if (view.GridControl.EmbeddedNavigator.Buttons.CustomButtons.Count > 0)
{
NavigatorButtonBase addButton = GetButtonByTag(view.GridControl.EmbeddedNavigator.Buttons, nameof(DetailButtons.Add));
if (addButton != null)
{
view.GridControl.EmbeddedNavigator.Buttons.DoClick(addButton);
view.FocusedColumn = view.VisibleColumns[0];//新增记录后,将焦点移动到第一列
e.Handled = true;
e.SuppressKeyPress = true;
return;//任务完成,不处理以下任何事务
}
}
}
}
}
/// <summary>
/// 移动到下一个可编辑的列, 若是当前行的最后一列,则定位下一行的第一个可编辑栏位.
/// </summary>
/// <param name="view">GridView</param>
/// <param name="activeControl">当前焦点的控件</param>
/// <param name="e"></param>
public static void MoveNextColumnOrRow(GridView view, GridColumn currentCol, KeyPressEventArgs e)
{
//若是当前行的最后一列,则定位下一行的第一个可编辑栏位.
if (IsLastEditableColumn(view, currentCol))
{
if (view.FocusedRowHandle + 1 < view.RowCount)
{
view.MoveNext();
view.FocusedColumn = view.VisibleColumns[0];
e.Handled = true;
}
}
else
{
//在当前行定位下一个可编辑栏位.
MoveNextColumn(view.FocusedColumn);
e.Handled = true;
}
}
/// <summary>
/// 移动到下一个可编辑的列
/// </summary>
/// <param name="currentCol"></param>
/// <param name="e"></param>
public static void MoveNextColumn(GridColumn currentCol)
{
try
{
for (int i = currentCol.VisibleIndex + 1; i < currentCol.View.VisibleColumns.Count; i++)
{
GridColumn colTemp = currentCol.View.VisibleColumns[i];
if (colTemp.OptionsColumn.AllowEdit && colTemp.OptionsColumn.ReadOnly == false)
{
currentCol.View.FocusedColumn = colTemp;
break;
}
}
}
catch
{
}
}
/// <summary>
/// 是否最后可编辑的列
/// </summary>
/// <param name="view">GridView</param>
/// <param name="focusedColumn">当前焦点的列</param>
/// <returns></returns>
public static bool IsLastEditableColumn(GridView view, GridColumn focusedColumn)
{
int idx = view.VisibleColumns.IndexOf(focusedColumn);
for (int i = view.VisibleColumns.Count - 1; i >= 0; i--)
{
GridColumn col = view.VisibleColumns[i];
//忽略不可编辑Column
if (!col.OptionsColumn.AllowEdit) continue;
if (idx != i)
return false; //非最后一个可编辑Column
else
return true;
}
return true;
}
/// <summary>
/// 根据GridView内置有焦点的控件,获取父级GridView
/// </summary>
/// <param name="activeControl">当前焦点的控件</param>
/// <returns></returns>
private static GridView GetGridViewByFocusEditor(Control activeControl)
{
while (activeControl.Parent != null)
{
if (activeControl.Parent is GridControl)
{
return (GridView)((GridControl)activeControl.Parent).FocusedView;
}
activeControl = activeControl.Parent;
}
return null;
}
/// <summary>
/// 检查当前控件是否GridView内置有焦点的控件
/// </summary>
/// <param name="activeControl">当前焦点的控件</param>
/// <returns></returns>
public static bool IsGridViewEditorFocus(Control activeControl)
{
bool isEditor = activeControl is TextBoxBase;
GridView view = GetGridViewByFocusEditor(activeControl);
return isEditor && (view != null);
}
/// <summary>
/// 按键盘向下键增加行KeyDownAddRow
/// </summary>
/// <param name="view"></param>
/// <param name="e"></param>
public static void OnGridViewAddNew(GridView view, KeyEventArgs e)
{
if (e.KeyValue == (char)Keys.Down && SystemSettings.Current.KeyDownAddRow && view.IsLastRow)
{
var column = view.FocusedColumn;
NavigatorButtonBase addButton = GetButtonByTag(view.GridControl.EmbeddedNavigator.Buttons, nameof(DetailButtons.Add));
if (addButton != null)
{
view.GridControl.EmbeddedNavigator.Buttons.DoClick(addButton);
view.FocusedColumn = column;
e.Handled = true;
}
}
}
public static NavigatorButtonBase GetButtonByTag(ControlNavigatorButtons buttons, string tag)
{
foreach (NavigatorButtonBase v in buttons.CustomButtons)
{
if (v.Tag == tag) return v;
}
return null;
}
}
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网