• DataGridView上下移动行及设置当前行


    //方法 上移 下移 删除 dGVshowProcess是一个DataGridView

            private void upOrdownOrDelete(string type)

            {           

                if (this.dGVshowProcess.CurrentRow == null)

                {

                    MessageBox.Show("请选择要需要操作的工序所在行");

                }

                else if(type=="del")//删

                {

                    if (MessageBox.Show("确定要删除吗?", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)

                    {

                        this.dGVshowProcess.Rows.Remove(this.dGVshowProcess.CurrentRow);

                    }

                }

                else if(type=="up")//上

                {

                    if (this.dGVshowProcess.CurrentRow.Index <= 0)

                    {

                        MessageBox.Show("此工序已在顶端,不能再上移!");

                    }

                    else

                    {                                 

                        int nowIndex = this.dGVshowProcess.CurrentRow.Index;

                        object[] _rowData = (this.dGVshowProcess.DataSource as DataTable).Rows[nowIndex].ItemArray;

                        (this.dGVshowProcess.DataSource as DataTable).Rows[nowIndex].ItemArray = (this.dGVshowProcess.DataSource as DataTable).Rows[nowIndex - 1].ItemArray;

                        (this.dGVshowProcess.DataSource as DataTable).Rows[nowIndex-1].ItemArray = _rowData;                  

                        this.dGVshowProcess.CurrentCell = this.dGVshowProcess.Rows[nowIndex - 1].Cells[0];//设定当前行

                    }

                }

                else if (type == "down")//下

                {

                    if (this.dGVshowProcess.CurrentRow.Index >= this.dGVshowProcess.Rows.Count-1)

                    {

                        MessageBox.Show("此工序已在底端,不能再下移!");

                    }

                    else

                    {                  

                        int nowIndex = this.dGVshowProcess.CurrentRow.Index;

                        object[] _rowData = (this.dGVshowProcess.DataSource as DataTable).Rows[nowIndex].ItemArray;

                        (this.dGVshowProcess.DataSource as DataTable).Rows[nowIndex].ItemArray = (this.dGVshowProcess.DataSource as DataTable).Rows[nowIndex + 1].ItemArray;

                        (this.dGVshowProcess.DataSource as DataTable).Rows[nowIndex+1].ItemArray = _rowData;

                        this.dGVshowProcess.CurrentCell = this.dGVshowProcess.Rows[nowIndex + 1].Cells[0];//设定当前行

                    }

                }

            }

  • 相关阅读:
    hello world !
    从数据库提取数据报错java.sql.SQLException: Column '4' not found.解决方法
    tomcat加载项目无法启动报错(Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/**])解决办法
    tomcat SERVER启动时did not find a matching property错误解决办法
    MVC与SSH(SSM)间的关系
    the resource is not on the build path of a Java project报错解决
    接口的作用
    eclipse error pages打红X的解决方法
    文本提交带单引号引起mysql报错
    五、HTML判断输入长度,体会字体颜色变化
  • 原文地址:https://www.cnblogs.com/MyBeN/p/3085130.html
Copyright © 2020-2023  润新知