datagridview中怎样将行上移或下移 请问datagridview中怎样将行上移或下移?当选中一行点击"向上"按钮,如果不是第一行,则向上移动,同时,当选中一行点击"向下"按钮,如果不是最后一行,则可向下移动。先谢过了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如果移动是变换选中项,直接模拟鼠标上下一动就可以了。如果是移动数据,看下边的代码,测试通过,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; } } this.dataGridView1.DataSource = dataSet;this.dataGridView1.DataMember = "test";CurrencyManager cm;cm = (this.BindingContext[dataSet, "test"] as CurrencyManager);private void button1_Click(object sender, EventArgs e) { if (cm.Position == (cm.Count -1)) cm.Position = 0; else cm.Position++; }http://topic.csdn.net/t/20060915/13/5024414.html 【C#界面】完美实现无毛边异形窗体 vmware vcenter server 无法获取Esx主机 uuid gridview翻页问题 求救,如何点击网页上的一个下载按钮,让文件下载到本地后直接打开,如EXCEL文件 菜鸟提个问题,如何在C#中连接数据库sql 或者access 提一个关于C# 反射的问题 按del键,删除datagrid中的行时,发生的事件是什么?怎么没有呢? 请大神推荐一下你认为最有用的5个设计模式名称 怎样结贴? 如何申明以下API调用? 接口问题,急 判断是否联网
如果是移动数据,看下边的代码,测试通过,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;
}
}
this.dataGridView1.DataMember = "test";
CurrencyManager cm;
cm = (this.BindingContext[dataSet, "test"] as CurrencyManager);
private void button1_Click(object sender, EventArgs e)
{
if (cm.Position == (cm.Count -1))
cm.Position = 0;
else
cm.Position++;
}
http://topic.csdn.net/t/20060915/13/5024414.html