首页 > 代码库 > 关于dataTable上移或下移

关于dataTable上移或下移

//记录上移或下移
        private void ReccordData(DataTable dt, string TypeValue,int index)
        {
            DataRow dr = dt.Rows[index]; //表示要移动的当前行对象
            string strValu = dr[0].ToString(); //当前行对象某一例的值(我这里默认是0列) strVal是记录此值
            dt.Rows.RemoveAt(index); //先删除它,在插入
            DataRow drnew = dt.NewRow();
            drnew["Engilsh"] = strValu;
            if (TypeValue =http://www.mamicode.com/= "UPEnglish") //说明是上移或者下移
            {
                dt.Rows.InsertAt(drnew, index - 1); //上移就在自已索引位置减1
            }
            else
            {
                dt.Rows.InsertAt(drnew, index+1); //反之下移就在当前位加1
            }
            gridViewInsertEnglishBind();// 绑定数据
        }
//下面是行命令事件
      protected void gridViewInTruEnglish_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "DeleteEnglish": // 删除
                   //  int row = ((GridViewRow)((ImageButton)sender).NamingContainer).RowIndex;
                     GridViewRow drvEnglishDelet = ((GridViewRow)(((ImageButton)(e.CommandSource)).Parent.Parent)); //此得出的值是表示那行被选中的索引值
                     dtInStroEnglish.Rows.RemoveAt(drvEnglishDelet.RowIndex);
                     gridViewInsertEnglishBind();// 绑定数据
 
 
                    break;
                case "DownEnglish": // 下降
                       GridViewRow drvDownEnglish = ((GridViewRow)(((ImageButton)(e.CommandSource)).Parent.Parent)); //此得出的值是表示那行被选中的索引值
                       int indexDown = drvDownEnglish.RowIndex;//当前行的索引
                       if (indexDown == dtInStroEnglish.Rows.Count-1) //表示最下面了,索引从0开始
                       {
                           return;
                       }
                       else
                       {
                            
                           ReccordData(dtInStroEnglish, "UPEnglish", indexDown);
 
                       }
 
                    break;
                case "UPEnglish": // 上升
                      GridViewRow drvUPEnglish = ((GridViewRow)(((ImageButton)(e.CommandSource)).Parent.Parent)); //此得出的值是表示那行被选中的索引值
                      int indexUp = drvUPEnglish.RowIndex;//当前行的索引
                      if (indexUp == 0) //当它本身位置是第一个当然是不可以上移的
                      {
                          return;
                      }
                      else
                      {                
                          ReccordData(dtInStroEnglish, "UPEnglish", indexUp);
                      }
                    break;
            }
        }

 

<br>

关于dataTable上移或下移