你DataGridView不是绑订到数据集上的吗 而且他们的数据是一致的,关键代码给你吧
object[] stu = new object[3];
stu[0] = ds.Tables["student"].Rows[4][0];
stu[1] = ds.Tables["student"].Rows[4][1];
stu[2] = ds.Tables["student"].Rows[4][2];
ds.Tables["student"].Rows.RemoveAt(4);
ds.Tables["student"].Rows.Add(stu);
我的数据库有3列,6条数据,这样就能实现LZ的要求,如果要持久化到数据库那用SqlDataAdapter的Update方法回写一次就是了 但是如果有主键 数据库好象会按主键自动排序
关键代码都给LZ了 你只需要变一点就OK了,希望LZ以后做题多查阅MSDN,对数据库和数据集的操作要做到相当熟练才行啊
我上面发给你的代码是直接把倒数第二行和最后一行交换,要想实现选哪行和他下面那行交换稍微改改就行啦,下面代码就是,在你下移按钮的Click事件里写
object[] obj = new object[ds.Tables["student"].Columns.Count];
int rowIndex = this.dataGridView1.CurrentRow.Index;
for (int i = 0; i < ds.Tables["student"].Columns.Count; i++)
obj[i] = ds.Tables["student"].Rows[rowIndex][i];
ds.Tables["student"].Rows.RemoveAt(rowIndex);
DataRow row = ds.Tables["student"].NewRow();
for (int i = 0; i < obj.Length; i++)
row[i] = obj[i];
ds.Tables["student"].Rows.InsertAt(row, rowIndex + 1);
其中ds.Tables["student"],这个student是我Fill时给的内存表名,LZ用你自己的,其他的基本都一样