C# datagridview中上下移动当前行且能将当前行放到首行和末尾

  1.         //下移一行
  2.         private void button2_Click( object sender, EventArgs e )
  3.         {
  4.             DataGridViewRow dr = dataGridView1.CurrentRow;
  5.             if ( dr.Index < dataGridView1.Rows.Count - 1 )
  6.             {
  7.                 int index = dr.Index;
  8.                 dataGridView1.Rows.Remove( dr );
  9.                 dataGridView1.Rows.Insert( index + 1, dr );
  10.                 dr.Selected = true;
  11.                 dataGridView1.CurrentCell = dr.Cells[0];
  12.             }
  13.         }
  14.         //上移一行
  15.         private void button3_Click( object sender, EventArgs e )
  16.         {
  17.             DataGridViewRow dr = dataGridView1.CurrentRow;
  18.             if ( dr.Index > 0 )
  19.             {
  20.                 int index = dr.Index;
  21.                 dataGridView1.Rows.Remove( dr );
  22.                 dataGridView1.Rows.Insert( index - 1, dr );
  23.                 dr.Selected = true;
  24.                 dataGridView1.CurrentCell = dr.Cells[0];
  25.             }
  26.         }
  27.         //首行
  28.         private void button4_Click( object sender, EventArgs e )
  29.         {
  30.             if ( dataGridView1.CurrentRow.Index != 0 )
  31.             {
  32.                 DataGridViewRow dr = dataGridView1.CurrentRow;
  33.                 dataGridView1.Rows.Remove( dr );
  34.                 dataGridView1.Rows.Insert(0, dr );
  35.                 dr.Selected = true;
  36.                 dataGridView1.CurrentCell = dr.Cells[0];
  37.             }
  38.         }
  39.         //末行
  40.         private void button5_Click( object sender, EventArgs e )
  41.         {
  42.             if ( dataGridView1.CurrentRow.Index 
  43.             {
  44.                 DataGridViewRow dr = dataGridView1.CurrentRow;
  45.                 dataGridView1.Rows.Remove( dr );
  46.                 dataGridView1.Rows.Insert( dataGridView1.Rows.Count , dr );
  47.                 dr.Selected = true;
  48.                 dataGridView1.CurrentCell = dr.Cells[0];
  49.             }
  50.         }

你可能感兴趣的:(C#)