移动DataGridView选中行

//向上移动
private void button4_Click(object sender, EventArgs e)
{//未选中if (dataGridView1.SelectedRows.Count==0){return;}//首行if (dataGridView1.SelectedRows[0].Index==0){return;}int selectedItemIndex = dataGridView1.SelectedRows[0].Index;//选中行的上一行的 空白副本DataGridViewRow dgvRow0 = (DataGridViewRow)dataGridView1.SelectedRows[0].Clone();//选中行的上一行的值存在对应副本里dgvRow0.Cells[0].Value = dataGridView1.Rows[selectedItemIndex - 1].Cells[0].Value;dgvRow0.Cells[1].Value = dataGridView1.Rows[selectedItemIndex - 1].Cells[1].Value;dgvRow0.Cells[2].Value = dataGridView1.Rows[selectedItemIndex - 1].Cells[2].Value;dgvRow0.Cells[3].Value = dataGridView1.Rows[selectedItemIndex - 1].Cells[3].Value;//用选中行覆盖掉上一行dataGridView1.Rows[selectedItemIndex - 1].Cells[0].Value = dataGridView1.Rows[selectedItemIndex].Cells[0].Value;dataGridView1.Rows[selectedItemIndex - 1].Cells[1].Value = dataGridView1.Rows[selectedItemIndex].Cells[1].Value;dataGridView1.Rows[selectedItemIndex - 1].Cells[2].Value = dataGridView1.Rows[selectedItemIndex].Cells[2].Value;dataGridView1.Rows[selectedItemIndex - 1].Cells[3].Value = dataGridView1.Rows[selectedItemIndex].Cells[3].Value;//用上一行的副本覆盖掉选中行dataGridView1.Rows[selectedItemIndex].Cells[0].Value = dgvRow0.Cells[0].Value;dataGridView1.Rows[selectedItemIndex].Cells[1].Value = dgvRow0.Cells[1].Value;dataGridView1.Rows[selectedItemIndex].Cells[2].Value = dgvRow0.Cells[2].Value;dataGridView1.Rows[selectedItemIndex].Cells[3].Value = dgvRow0.Cells[3].Value;//将移动后的行设为选中行dataGridView1.Rows[selectedItemIndex - 1].Selected = true;
}
//向下移动
private void button5_Click(object sender, EventArgs e)
{//未选中if (dataGridView1.SelectedRows.Count == 0){return;}//最后一行if (dataGridView1.SelectedRows[0].Index == dataGridView1.RowCount-1){return;}int selectedItemIndex = dataGridView1.SelectedRows[0].Index;//选中行的下一行的 空白副本DataGridViewRow dgvRow0 = (DataGridViewRow)dataGridView1.SelectedRows[0].Clone();//选中行的下一行的值存在对应副本里dgvRow0.Cells[0].Value = dataGridView1.Rows[selectedItemIndex + 1].Cells[0].Value;dgvRow0.Cells[1].Value = dataGridView1.Rows[selectedItemIndex + 1].Cells[1].Value;dgvRow0.Cells[2].Value = dataGridView1.Rows[selectedItemIndex + 1].Cells[2].Value;dgvRow0.Cells[3].Value = dataGridView1.Rows[selectedItemIndex + 1].Cells[3].Value;//用选中行覆盖掉下一行dataGridView1.Rows[selectedItemIndex + 1].Cells[0].Value = dataGridView1.Rows[selectedItemIndex].Cells[0].Value;dataGridView1.Rows[selectedItemIndex + 1].Cells[1].Value = dataGridView1.Rows[selectedItemIndex].Cells[1].Value;dataGridView1.Rows[selectedItemIndex + 1].Cells[2].Value = dataGridView1.Rows[selectedItemIndex].Cells[2].Value;dataGridView1.Rows[selectedItemIndex + 1].Cells[3].Value = dataGridView1.Rows[selectedItemIndex].Cells[3].Value;//用下一行的副本覆盖掉选中行dataGridView1.Rows[selectedItemIndex].Cells[0].Value = dgvRow0.Cells[0].Value;dataGridView1.Rows[selectedItemIndex].Cells[1].Value = dgvRow0.Cells[1].Value;dataGridView1.Rows[selectedItemIndex].Cells[2].Value = dgvRow0.Cells[2].Value;dataGridView1.Rows[selectedItemIndex].Cells[3].Value = dgvRow0.Cells[3].Value;//将移动后的行设为选中行dataGridView1.Rows[selectedItemIndex + 1].Selected = true;}

效果如下:

移动DataGridView选中行相关推荐

  1. C#中datagridview选中行后textbox显示选中的内容

    我想让datagridview中某一行被选中时,textbox中显示选中的值,datagridview的选中模式是整行: this.dataGridView1.SelectionMode = Data ...

  2. C#DataGridView选中行赋值刷新了,但是列表没刷新

    前久我遇到了一个问题就是2个控件,一个按钮是点击了它选择下一行,另外一个是点击了它就选择下一行,但是后面写好以后点击了上下行主表的DataGridView选中行还是没有变换,但是关联它的从表的Data ...

  3. dataGridView选中行的语法

    for (int i = 0; i < this.dataGridView1.SelectedRows.Count; i++)//遍历所有选中的行 {this.dataGridView1.Sel ...

  4. 将DataGridView选中行对象(DataGridViewRow)转为DataTable的行对象

    将DataGridView中选择的行存为一个DataTable,就要将DataGridViewRow对象转为DataRow对象. 具体做法如下: private void btn_in_all_Cli ...

  5. 基于DataGridView选中行获取图片显示于PictureBox中

    第一步:将需要的素材照片放在项目所在文件的bin目录下的Debug目录下 也可以在bin目录下的Debug目录下新建一个文件夹,如下图所示新建了一个Img文件,将素材图片放在Img文件中 第二步:创建 ...

  6. DataGridView 选中行、列、单元格

    基本数据绑定 用 DataSource 属性来绑定 DataTable 对象(或从 DataTable 派生的对象). Dim ds As DataSet = GetDataSet() DataGri ...

  7. java删除选中行数据库数据_删除DataGridView选中行并更新数据库

    前面写过一篇文章是DataGridView控件显示数据的,DataGridView在与数据库打交道时会经常出现,也很实用.通过DataGridView对数据库进行更改和查询都比较方便. 这里我们需要用 ...

  8. DataGridView 选中行

    说明: (1)命名 DataGridView 名称:dgvStockFirst 行索引:recordIndex (2)设置DataGridView属性: SelectionMode=FullRowSe ...

  9. C# 删除DataGridView选中行

    //普通删除方式 int i = dgvAwaitX.CurrentRow.Index; DataGridViewRow row = dgvAwaitX.Rows[i]; dgvAwaitX.Rows ...

最新文章

  1. 在markdown里面插入特殊的符号
  2. 铲屎官福音:汪星人和喵星人的小情绪,AI 可以识别了
  3. Zookeeper原生客户端
  4. java.util.concurrent BlockingQueue详解
  5. 手机linux编译器,在linux上交叉编译helloWorld到android手机上去
  6. 【深度学习torch——error】——“xxx.pt is a zip archive(did you mean to use torch.jit.load()?)
  7. jquery 取值 数值_jQuery – 使用正则表达式获取数据值
  8. 【高校宿舍管理系统】第七章 机构管理和功能菜单权限分配
  9. Visual Studio中Debug和Release的区别
  10. Python链表学习
  11. Android开发笔记(一百二十二)循环器视图RecyclerView
  12. 【Java从0到架构师】JS_jQuery_BootStrap
  13. Avast注册以及更新
  14. 基于51单片机WiFi温湿度远程控制
  15. html怎么设置顶部导航栏,CSS+HTML如何实现顶部导航栏
  16. 为什么今年高考数学又出编程题?
  17. Emacs第一天安装和简单配置
  18. python实现动态规划0-1背包问题
  19. JAVA图形化打字小游戏
  20. Mediawiki环境搭建

热门文章

  1. django app注册过程
  2. 在网络笔记本电脑或MID上测试驱动Moblin
  3. 微信开放平台 网站应用 第三方微信登录 回调地址导致的错误如何解决
  4. 自动填写个人信息(问卷星)
  5. 2021年施工升降机司机(建筑特殊工种)考试资料及施工升降机司机(建筑特殊工种)证考试
  6. 饮食误区:多喝茶可以预防心脑血管疾病?
  7. java计算机毕业设计健康饮食信息管理系统MyBatis+系统+LW文档+源码+调试部署
  8. h3c snmp配置实例_H3C SNMP配置解析
  9. H3C模拟器DHCP两台路由器配置实验详细步骤
  10. 无法将“npm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称 。请检查名称