移动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. .net卸载程序制作
  2. RSA加密算法——密码学笔记(四)
  3. CKFinder根据用户设置权限,不同用户有自己的私有的、独立的文件夹
  4. Python开发【第五篇】迭代器、生成器、递归函数、二分法
  5. bzoj3202:[Sdoi2013]项链
  6. python和excel能结合应用吗_通过Python在Excel中使用机器学习
  7. 【原创】基于NIOS II的ADS1256 SPI驱动
  8. textarea不可拖动
  9. 斯坦福 AIMI 打造世界更大的开源医学数据集
  10. 4. 正则表达式(4)
  11. 005 IK-analyzer添加搜狗词库
  12. 无盘服务器uefi启动,易乐游无盘网吧客户机使用UEFI启动教程
  13. ubuntu 通过ssh链接ARM板 及 IMX6使用调试串口通信
  14. 网络存储专有名词介绍
  15. 李佳琦、薇娅们的残酷生存物语
  16. 【转】【转】 图像处理与计算机视觉的经典书籍
  17. 220609_Efficient Uncertainty-aware Decision-making for Automated Driving Using Guided Branching
  18. VS2019切换中英文
  19. DAU/MAU?UGC?
  20. 外汇平台正规排行榜 Flyerinternational稳居前三

热门文章

  1. 面向对象1---this、构造方法、方法重载,静态,权限、getter和sett、继承、super、方法重写、多态
  2. 预测分析:R语言实现2.3 多元线性回归
  3. 杂项-Map:高德地图
  4. Leetcode 宝石与石头 C++
  5. if js 判断成绩等级_JS 条件语句
  6. Excel如何利用choose函数判断日期所处季度
  7. 如何通过twitter群推王引流到listing
  8. [ 数据通信面试篇 ] 数通面试题大集合(详解),看完直怼面试官(五)
  9. C语言 — 必学的编程基础语法,入门就从这里开始!
  10. 2023年建议自考还是成考 两者哪个含金量高