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 <dataGridView1.Rows.Count-1 )
  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# ,可以通过以下步骤将 DataGridView 的所有记录更新到数据表: 1. 获取数据表的数据适配器和数据表对象。 2. 使用数据适配器的 Fill 方法获取数据表的数据到 DataSet 。 3. 在 DataGridView 编辑、添加或删除记录。 4. 使用数据适配器的 Update 方法将 DataSet 的更改保存回数据表。 以下是一个示例代码: ``` // 获取数据表的数据适配器和数据表对象 string connectionString = "your_connection_string_here"; string tableName = "your_table_name_here"; SqlDataAdapter dataAdapter = new SqlDataAdapter("SELECT * FROM " + tableName, connectionString); SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter); DataTable dataTable = new DataTable(); // 使用数据适配器的 Fill 方法获取数据表的数据到 DataSet dataAdapter.Fill(dataTable); // 将 DataTable 绑定到 DataGridView dataGridView1.DataSource = dataTable; // 在 DataGridView 编辑、添加或删除记录 // 使用数据适配器的 Update 方法将 DataSet 的更改保存回数据表 dataAdapter.Update(dataTable); ``` 在上述示例,我们首先获取了数据适配器和数据表对象,然后使用数据适配器的 Fill 方法将数据表的数据填充到 DataTable ,并将 DataTable 绑定到 DataGridView 。接着,在 DataGridView 编辑、添加或删除操作后,我们使用数据适配器的 Update 方法将更改保存回数据表
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值