惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

小众软件
小众软件
C
Check Point Blog
Vercel News
Vercel News
Y
Y Combinator Blog
G
Google Developers Blog
P
Proofpoint News Feed
WordPress大学
WordPress大学
MongoDB | Blog
MongoDB | Blog
博客园 - 司徒正美
Last Week in AI
Last Week in AI
博客园 - 【当耐特】
N
Netflix TechBlog - Medium
L
LangChain Blog
V
V2EX
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
大猫的无限游戏
大猫的无限游戏
D
DataBreaches.Net
博客园_首页
B
Blog RSS Feed
The Cloudflare Blog
MyScale Blog
MyScale Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Security Blog
Microsoft Security Blog

博客园 - ccczqh

Entity Framework对NULL值的处理 整理一下Entity Framework的查询 sqlhelper 使用事务实例 SqlHelper 类实现详细信息 SQL Server 2008R2中增加了新的智能 DateTime 数字型 C# tostring()汇总 DataGridView 列宽和行高自动调整的设定 datagridview,第一列前没有灰的一列 DataGridView取消默认选中行 dataGridView空白列,默认选中行的背景色,dataGridView中加入复选框勾选状态的更改 C# winform DataGridView 常见属性 转ADO.NET Entity Framework 入门示例向导(附Demo程序下载) c#加密解密 大数据类型通过存储过程保存数据(clob,blob) WinForm 下实现一个自动关闭的MessageBox C#简单游戏外挂制作(以Warcraft Ⅲ为例) [WebBrowser][Cookie] Document.Cookie 取得的Cookies不完整問題 C# DllImport的用法
C# dataGridView上下移动选中行
ccczqh · 2013-02-12 · via 博客园 - ccczqh

/*DataGridView 实现行[Row]的上下移动,我这里用到了SelectedRows[0],而没用CurrentRow是有原因的
   主要是这两段代码:
   dataGridView1.Rows[rowIndex - 1].Selected = true;
   dataGridView1.Rows[rowIndex].Selected = false;
   这两行代码大家因该都能看懂,移上去的哪行选中状态,移下去的的取消选中状态.
   如果我用dataGridView1.CurrentRow.Cell[0].Value 他取得的值仍然是rowIndex索引行的值
    要使用SelectedRows[0] ,就必须设置这个属性:dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

   实现原理:就是上下两行,把单元格中的值进行交换...呵呵表面上看去是向上,下移动了

   不知道大家还有什么好的选中方法没...请多多指教 

*/

    private void Form3_Load(object sender, EventArgs e)
    {
      //........得到DataTable的代码省略....
      dataGridView1.DataSource = dt;
    }

    private void button1_Click(object sender, EventArgs e)  //向上移动
    {
      int rowIndex = dataGridView1.SelectedRows[0].Index;  //得到当前选中行的索引

      if(rowIndex == 0)
      {
        MessageBox.Show("已经是第一行了!");
        return;
      }

      List<string> list = new List<string>();
      for (int i = 0; i < dataGridView1.Columns.Count; i++)
      {
        list.Add(dataGridView1.SelectedRows[0].Cells[i].Value.ToString());   //把当前选中行的数据存入list数组中
      }

      for (int j = 0; j < dataGridView1.Columns.Count; j++)
      {
          dataGridView1.Rows[rowIndex].Cells[j].Value = dataGridView1.Rows[rowIndex - 1].Cells[j].Value;
          dataGridView1.Rows[rowIndex - 1].Cells[j].Value = list[j].ToString();
      }
      dataGridView1.Rows[rowIndex - 1].Selected = true;
      dataGridView1.Rows[rowIndex].Selected = false;
    }

    private void button2_Click(object sender, EventArgs e)  //向下移动
    {
      int rowIndex = dataGridView1.SelectedRows[0].Index;  //得到当前选中行的索引

      if (rowIndex == dataGridView1.Rows.Count -1)
      {
        MessageBox.Show("已经是最后一行了!");
        return;
      }

      List<string> list = new List<string>();
      for (int i = 0; i < dataGridView1.Columns.Count; i++)
      {
        list.Add(dataGridView1.SelectedRows[0].Cells[i].Value.ToString());   //把当前选中行的数据存入list数组中
      }

      for (int j = 0; j < dataGridView1.Columns.Count; j++)
      {
        dataGridView1.Rows[rowIndex].Cells[j].Value = dataGridView1.Rows[rowIndex + 1].Cells[j].Value;
        dataGridView1.Rows[rowIndex + 1].Cells[j].Value = list[j].ToString();
      }
      dataGridView1.Rows[rowIndex + 1].Selected = true;
      dataGridView1.Rows[rowIndex].Selected = false;
    }