DataGridView(WinForm)中实现行的上下移动。模拟了20条数据,绑定到DataGridView,点击向上按钮,数据行向上移动一行,点击向下,数据行向下移动一行。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DataGridViewDemo
{
??? public partial class Form1 : Form
??? {
??????? List<UserInfo> dataList = null;
??????? /// <summary>
??????? /// Initializes a new instance of the <see cref="Form1"/> class.
??????? /// </summary>
??????? public Form1()
??????? {
??????????? InitializeComponent();
??????????? InitData();
??????? }
??????? private void InitData()
??????? {
??????????? //初始化20条数据
??????????? dataList = new List<UserInfo>();
??????????? for (int i = 1; i <= 20; i++)
??????????? {
??????????????? UserInfo objUserInfo = new UserInfo();
??????????????? objUserInfo.UID = i;
??????????????? objUserInfo.UName = "姓名" + i;
??????????????? dataList.Add(objUserInfo);
??????????? }
??????????? //绑定数据到DataGridView
??????????? dataGridView1.DataSource = dataList;
??????? }
??????? /// <summary>
??????? /// 点击向上按钮触发事件
??????? /// </summary>
??????? /// <param name="sender"></param>
??????? /// <param name="e"></param>
??????? private void button1_Click(object sender, EventArgs e)
??????? {
??????????? //获取当前行
??????????? int currentRowIndex = dataGridView1.CurrentRow.Index;
??????????? //如果当前行不是第一行
??????????? if (currentRowIndex > 0)
??????????? {
??????????????? //获取当前行的上一行的索引
??????????????? int nextRowIndex = currentRowIndex - 1;
??????????????? //当前行绑定的数据对象
??????????????? UserInfo currentUserInfo = dataList[currentRowIndex];
??????????????? //下一行绑定的数据对象
??????????????? UserInfo nextUserInfo = dataList[nextRowIndex];
??????????????? //将数据对象位置互换
??????????????? dataList[currentRowIndex] = nextUserInfo;
??????????????? dataList[nextRowIndex] = currentUserInfo;
??????????????? //重新绑定数据
??????????????? dataGridView1.DataSource = dataList;
??????????????? //当前行不选中
??????????????? dataGridView1.Rows[currentRowIndex].Selected = false;
??????????????? //选中当前行的下一行
??????????????? dataGridView1.Rows[nextRowIndex].Selected = true;
??????????????? //当前行设置为当前行的下一行
??????????????? dataGridView1.CurrentCell = dataGridView1.Rows[nextRowIndex].Cells[0];
??????????????? //刷新界面
??????????????? dataGridView1.Refresh();
??????????? }
??????? }
??????? private void button2_Click(object sender, EventArgs e)
??????? {
??????????? //获取当前行
??????????? int currentRowIndex = dataGridView1.CurrentRow.Index;
??????????? //如果当前行不是最后一行
??????????? if (currentRowIndex < dataGridView1.RowCount - 1)
??????????? {
??????????????? //获取当前行的下一行的索引
??????????????? int nextRowIndex = currentRowIndex + 1;
??????????????? //当前行绑定的数据对象
??????????????? UserInfo currentUserInfo = dataList[currentRowIndex];
??????????????? //下一行绑定的数据对象
??????????????? UserInfo nextUserInfo = dataList[nextRowIndex];
??????????????? //将数据对象位置互换
??????????????? dataList[currentRowIndex] = nextUserInfo;
??????????????? dataList[nextRowIndex] = currentUserInfo;
??????????????? //重新绑定数据
??????????????? dataGridView1.DataSource = dataList;
??????????????? //当前行不选中
??????????????? dataGridView1.Rows[currentRowIndex].Selected = false;
??????????????? //选中当前行的下一行
??????????????? dataGridView1.Rows[nextRowIndex].Selected = true;
??????????????? //当前行设置为当前行的下一行
??????????????? dataGridView1.CurrentCell = dataGridView1.Rows[nextRowIndex].Cells[0];
??????????????? //刷新界面
??????????????? dataGridView1.Refresh();
??????????? }
??????? }
??? }
??? public class UserInfo
??? {
??????? public int UID
??????? {
??????????? get;
??????????? set;
??????? }
??????? public string UName
??????? {
??????????? get;
??????????? set;
??????? }
??? }
}
关键字: datagridview 移动行

目前有1 条评论
发表评论
相关文章
国内AI资源汇总,AI聊天、AI绘画、AI写作、AI视频、AI设计、AI编程、AI音乐等,国内顺畅访问,无需科学上网。
扫码或点击进入:萤火AI大全
文章分类
最新评论