波斯马BOSSMA Information Technology

DataGridView(WinForm)中实现行的上下移动

发布时间:2010年8月19日 / 分类:ASP.NET, WinForm / 20,290 次浏览 / 评论

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(WinForm)中实现行的上下移动

关键字:

建议订阅本站,及时阅读最新文章!
【上一篇】 【下一篇】

目前有1 条评论

发表评论