数据表格
客户需求
扫码枪录入表号(相当于键盘输入表号再回车),未选中的行(表中:2、3、6、10行),自动跳过。
实现代码
#region 重写Form ProcessCmdKey方法
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (!rtMonitorDataGrid.Focused) return false; // 如果焦点不在当前表格View返回false,防止报错
if (keyData == Keys.Enter) // 监听回车事件
{
try
{
checkRowValue(rtMonitorDataGrid.CurrentRow.Index);
}
catch (Exception e)
{
LogUtil.Instance.LogDebug(e.Message);
}
}
return base.ProcessCmdKey(ref msg, keyData);
}
private void checkNextRowValue(int currentRowIndex)
{
// 检查下一行是否被选中
if (!(bool)rtMonitorDataGrid.Rows[currentRowIndex+ 1].Cells[0].Value)
{
SendKeys.Send("{Down}"); // 焦点移至下一行
checkNextRowValue(rowIndex + 1); // 递归继续检查下一行
}
}
#endregion