zoukankan      html  css  js  c++  java
  • dataGridView使用指南系列一、回车换行或换列完美解决方案

    在使用datagridview控件时,默认按回车是跳转到下一行的当前列的,要想让按回车跳转到同一行的下一列该怎么做呢?

    百度搜索了一下,大都是使用该控件的key_down事件和CellEndEdit进行处理,我都试验了一下,全都不行,不能达到预期的效果,而且使原来的方向键的功能也搞乱了。

    找了半天,最后终于找到一个有效的方法(注:不是我发明的),重载窗体的ProcessCmdKey事件,代码如下:

    复制代码
    代码
    bool enterkey;
    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
    enterkey = false;

    if (keyData == Keys.Enter) //监听回车事件
    {
    if (this.gvGzb.IsCurrentCellInEditMode) //如果当前单元格处于编辑模式
    {
    enterkey = true; //把是否点击按钮设置为真

    if (btnSetEnter.Text != "竖")
    if (gvGzb.CurrentCell.RowIndex == gvGzb.Rows.Count - 1)
    {
    //SendKeys.Send("{Up}");
    SendKeys.Send("{Tab}");
    }
    else
    {
    SendKeys.Send("{Up}");
    SendKeys.Send("{Tab}");
    }
    }


    }

    //继续原来base.ProcessCmdKey中的处理
    return base.ProcessCmdKey(ref msg, keyData);
    }
    复制代码

    以上代码只处理编辑状态下的回车事件,在普通状态下的回车事件就要用key_down事件处理了:

    复制代码
    代码
    private void gvGzb_KeyDown(object sender, KeyEventArgs e)
    {
    if ((e.KeyCode == Keys.Return) && (btnSetEnter.Text != "竖"))
    {
    SendKeys.Send("{Tab}");
    e.Handled = true;
    if ((gvGzb.FirstDisplayedScrollingColumnHiddenWidth > 0) && !gvGzb.Columns[gvGzb.SelectedCells[0].ColumnIndex].Frozen)
    {
    gvGzb.FirstDisplayedScrollingColumnIndex = gvGzb.SelectedCells[0].ColumnIndex;
    }
    }

    }
    复制代码

    上面的FirstDisplayedScrollingColumnIndex属性设置,是为了在datagirdview的列很多的情况下,按回车换列将滚动条后面的列自动显示出来,如下图,我用net做的简易工资管理系统,有很多项,如果不设置的话,最后一列只能看到一半。

    提供这个方法的那位仁兄,不知为什么还要处理Cell_Parsing事件,我这里不需要已经发觉很完美了。

  • 相关阅读:
    基于摸板匹配的目標跟蹤算法
    spoj 2713 Can you answer these queries IV
    zoj 3633 Alice's present
    hdu 3642 Get The Treasury
    poj 1195 Mobile phones
    poj 2760 End of Windless Days
    zoj 3540 Adding New Machine
    spoj 1716 Can you answer these queries III
    spoj 1043 Can you answer these queries I
    spoj 2916 Can you answer these queries V
  • 原文地址:https://www.cnblogs.com/soundcode/p/10169802.html
Copyright © 2011-2022 走看看