• c1TrueDBGrid--使用键盘上的上下键来移动c1TrueDBGrid 表格内的数据(实现了上下移动)


    1.使用KeyDown、KeyPress事件好像不可以实现,所以使用了KeyUp事件

    //c1TrueDBGrid1_KeyUp事件

    private void c1TrueDBGrid1_KeyUp(object sender, KeyEventArgs e)

    {

    //获取c1TrueDBGrid1的数据
    DataTable dt = c1TrueDBGrid1.DataSource as DataTable;
    //当前行
    int rowIndex = this.c1TrueDBGrid1.Row;
    //往下移动
    if (e.KeyCode == Keys.Down)
    {
    if (rowIndex > 0 && rowIndex < dt.Rows.Count)
    {
    DataRow newdata = dt.NewRow();
    DataRow olddata = dt.NewRow();
    //获取当前行的数据
    newdata.ItemArray = dt.Rows[rowIndex].ItemArray;
    //获取当前行的上一行数据
    olddata.ItemArray = dt.Rows[rowIndex - 1].ItemArray;
    //移除当前行
    dt.Rows.RemoveAt(rowIndex);
    //移除当前行的上一行
    dt.Rows.RemoveAt(rowIndex - 1);

    //移除数据后插入新数据
    dt.Rows.InsertAt(newdata, rowIndex - 1);
    dt.Rows.InsertAt(olddata, rowIndex);
    dt.AcceptChanges();

    //设置选中行
    this.c1TrueDBGrid1.SetActiveCell(rowIndex, 1);
    }
    }
    //往上移动
    else if (e.KeyCode == Keys.Up)
    {
    //当前行
    //int rowIndex = this.c1TrueDBGrid1.Row;
    if (rowIndex > 0)
    {
    DataRow newdata = dt.NewRow();
    DataRow olddata = dt.NewRow();
    //获取当前行的数据
    newdata.ItemArray = dt.Rows[rowIndex + 1].ItemArray;
    //获取当前行的下一行数据
    olddata.ItemArray = dt.Rows[rowIndex].ItemArray;
    //移除当前行
    dt.Rows.RemoveAt(rowIndex);

    //移除数据后插入新数据
    dt.Rows.InsertAt(newdata, rowIndex);
    //dt.AcceptChanges();
    //移除当前行的下一行
    dt.Rows.RemoveAt(rowIndex + 1);

    //移除数据后插入新数据
    dt.Rows.InsertAt(olddata, rowIndex + 1);
    dt.AcceptChanges();
    this.c1TrueDBGrid1.SetActiveCell(rowIndex, 1);
    }
    }

    }

  • 相关阅读:
    数组的空位
    数组方法之pop
    数组方法之push
    深拷贝
    浅拷贝
    手动编写用于react项目开发的的webpack配置文件
    ES6:export default 和 export 区别
    JS基础算法题(二)
    Linux系统下用户如何膝盖FTP用户密码
    Sublime Text 3 安装插件与快捷键总结
  • 原文地址:https://www.cnblogs.com/jinjingBlog/p/10600787.html
Copyright © 2020-2023  润新知