DataGridView单元格编辑时,按上下方向键无法触发KeyDown事件,各位大哥大姐帮帮忙

时间:2021-03-30 19:25:24
源码如下:
       private DataGridViewTextBoxEditingControl EditingControl = null;

        private void dgvMain_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            
            if (this.dgvMain.CurrentCell.RowIndex > -1)
            {
                if (e.Control is DataGridViewTextBoxEditingControl)
                {
                    this.EditingControl = (DataGridViewTextBoxEditingControl)e.Control;
                    //增加委托处理
                    this.EditingControl.KeyDown += new KeyEventHandler(this.EditingControl_KeyDown);               
                }

            }

        }

        private void EditingControl_KeyDown(object sender, KeyEventArgs e)
        {
           //业务处理
           //按上下键时就是不触发该事件
        }

        private void dgvMain_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (this.EditingControl != null)
            {
                EditingControl.KeyDown -= new KeyEventHandler(this.EditingControl_KeyDown);
                this.EditingControl = null;
            }
        }

4 个解决方案

#1


高手了?? 
哪位老大 给点提示 

#2


PreviewKeyDownEvent 可以触发上下键的事件

this.EditingControl.PreviewKeyDown += new PreviewKeyDownEventHandler(this.EditingControl_PreviewKeyDownEvent);
this.EditingControl.KeyDown += new KeyEventHandler(this.EditingControl_KeyDown);

private void EditingControl_PreviewKeyDownEvent(object sender, PreviewKeyDownEventArgs e)
{
    //业务处理 
    //按上下键时触发该事件 
}

private void EditingControl_KeyDown(object sender, KeyEventArgs e)
{
    //MessageBox.Show("dsdss");
    //业务处理 
    //按上下键时就是不触发该事件 
}

#3


非常感谢 lzsh0622 
用PreviewKeyDown就可以 

#4


哎!没想到今天运气这么好,百度一下5分钟不到就搞定了;感谢2楼

#1


高手了?? 
哪位老大 给点提示 

#2


PreviewKeyDownEvent 可以触发上下键的事件

this.EditingControl.PreviewKeyDown += new PreviewKeyDownEventHandler(this.EditingControl_PreviewKeyDownEvent);
this.EditingControl.KeyDown += new KeyEventHandler(this.EditingControl_KeyDown);

private void EditingControl_PreviewKeyDownEvent(object sender, PreviewKeyDownEventArgs e)
{
    //业务处理 
    //按上下键时触发该事件 
}

private void EditingControl_KeyDown(object sender, KeyEventArgs e)
{
    //MessageBox.Show("dsdss");
    //业务处理 
    //按上下键时就是不触发该事件 
}

#3


非常感谢 lzsh0622 
用PreviewKeyDown就可以 

#4


哎!没想到今天运气这么好,百度一下5分钟不到就搞定了;感谢2楼