zoukankan      html  css  js  c++  java
  • 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);
    }
    }

    }

  • 相关阅读:
    学习工具
    Qt 之 QApplication
    Qt中常用的类
    关于在Qt里让程序休眠一段时间的方法总结
    Qt setWindow setViewPort
    ajax回调数据 Structs has detected an unhandled exception 问题
    Struts2配置拦截器自定义栈时抛异常:Unable to load configuration.
    es6之map解构数组去重
    ES6之对象的方法
    ES6之genorator和yield使用(迭代器)
  • 原文地址:https://www.cnblogs.com/jinjingBlog/p/10600787.html
Copyright © 2011-2022 走看看