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

    }

  • 相关阅读:
    BNUOJ 12756 Social Holidaying(二分匹配)
    HDU 1114 Piggy-Bank(完全背包)
    HDU 2844 Coins (多重背包)
    HDU 2602 Bone Collector(01背包)
    HDU 1171 Big Event in HDU(01背包)
    HDU 2571 命运 (入门dp)
    HDU 1069 Monkey and Banana(最长递减子序列)
    HDU 1160 FatMouse's Speed (最长上升子序列)
    HDU 2594 KMP
    POJ 3783 Balls --扔鸡蛋问题 经典DP
  • 原文地址:https://www.cnblogs.com/jinjingBlog/p/10600787.html
Copyright © 2011-2022 走看看