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);
    }
    }

    }

  • 相关阅读:
    Java常见问题汇总
    前端url参数中带有callback并产生错误
    shiro中ecache-core版本引起的异常
    深入SpringMVC注解
    导出表格数据到excel并下载(HSSFWorkbook版)
    layui数据表格及分页
    签名的生成
    程序的健壮性Robustness
    ASP.NET MVC中注册Global.asax的Application_Error事件处理全局异常
    生成二维码功能
  • 原文地址:https://www.cnblogs.com/jinjingBlog/p/10600787.html
Copyright © 2011-2022 走看看