如何让winfrom的datagridview行数据上下移动

zl4460072 2010-03-19 02:51:12
我百度了半天也没有正确的方法,特来求教啦
代码:

private void button1_Click(object sender, EventArgs e)
{
string connStr = "server=localhost;database=zilong;uid=root;pwd=4460072";
using (MySqlConnection conn = new MySqlConnection(connStr))
{
ds = new DataSet();
string sql = "select * from student ";
MySqlDataAdapter da = new MySqlDataAdapter(sql,conn);
da.Fill(ds,"stu");
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "stu";
}
}

private void button2_Click(object sender, EventArgs e)
{
string s = dataGridView1.CurrentRow.Index.ToString();
label1.Text = s;
....
}

怎么样按了button2可以让当前选中的行数据整体移动呢???
...全文
440 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenjunhui00 2011-07-20
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 wxm3630478 的回复:]
打不开,帮你全部复制下来
C# code

/*DataGridView 实现行[Row]的上下移动,我这里用到了SelectedRows[0],而没用CurrentRow是有原因的
主要是这两段代码:
dataGridView1.Rows[rowIndex - 1].Selected = true;
dataGridView1.Rows[rowIn……
[/Quote]
zouzouol 2010-08-31
  • 打赏
  • 举报
回复
学习,学习
zdk8105 2010-06-15
  • 打赏
  • 举报
回复
学习 了啊
zilong4460072 2010-03-19
  • 打赏
  • 举报
回复

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private DataTable dt;
private void Form1_Load(object sender, EventArgs e)
{
string connStr = "server=localhost;database=zilong;uid=root;pwd=4460072";
using (MySqlConnection conn = new MySqlConnection(connStr))
{
dt = new DataTable();
string sql = "select * from student ";
MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);
da.Fill(dt);
dataGridView1.DataSource = dt;
}
}


//上移
private void button2_Click(object sender, EventArgs e)
{
int rowIndex = dataGridView1.CurrentRow.Index;
if (rowIndex == 0)
{
MessageBox.Show("这已经是第一行了");
return;
}
List<string> list = new List<string>();
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
list.Add(dataGridView1.SelectedRows[0].Cells[i].Value.ToString()); //把当前选中行的数据存入list数组中
}
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
dataGridView1.Rows[rowIndex].Cells[j].Value = dataGridView1.Rows[rowIndex - 1].Cells[j].Value;
dataGridView1.Rows[rowIndex - 1].Cells[j].Value = list[j].ToString();
}
dataGridView1.Rows[rowIndex - 1].Selected = true;
dataGridView1.Rows[rowIndex].Selected = false;


}
}
wxm3630478 2010-03-19
  • 打赏
  • 举报
回复
打不开,帮你全部复制下来

/*DataGridView 实现行[Row]的上下移动,我这里用到了SelectedRows[0],而没用CurrentRow是有原因的
主要是这两段代码:
dataGridView1.Rows[rowIndex - 1].Selected = true;
dataGridView1.Rows[rowIndex].Selected = false;
这两行代码大家因该都能看懂,移上去的哪行选中状态,移下去的的取消选中状态.
如果我用dataGridView1.CurrentRow.Cell[0].Value 他取得的值仍然是rowIndex索引行的值

要使用SelectedRows[0] ,就必须设置这个属性:dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

实现原理:就是上下两行,把单元格中的值进行交换...呵呵表面上看去是向上,下移动了

不知道大家还有什么好的选中方法没...请多多指教

*/

private void Form3_Load(object sender, EventArgs e)
{
//........得到DataTable的代码省略....
dataGridView1.DataSource = dt;
}

private void button1_Click(object sender, EventArgs e) //向上移动
{
int rowIndex = dataGridView1.SelectedRows[0].Index; //得到当前选中行的索引

if(rowIndex == 0)
{
MessageBox.Show("已经是第一行了!");
return;
}

List<string> list = new List<string>();
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
list.Add(dataGridView1.SelectedRows[0].Cells[i].Value.ToString()); //把当前选中行的数据存入list数组中
}

for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
dataGridView1.Rows[rowIndex].Cells[j].Value = dataGridView1.Rows[rowIndex - 1].Cells[j].Value;
dataGridView1.Rows[rowIndex - 1].Cells[j].Value = list[j].ToString();
}
dataGridView1.Rows[rowIndex - 1].Selected = true;
dataGridView1.Rows[rowIndex].Selected = false;
}

private void button2_Click(object sender, EventArgs e) //向下移动
{
int rowIndex = dataGridView1.SelectedRows[0].Index; //得到当前选中行的索引

if (rowIndex == dataGridView1.Rows.Count -1)
{
MessageBox.Show("已经是最后一行了!");
return;
}

List<string> list = new List<string>();
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
list.Add(dataGridView1.SelectedRows[0].Cells[i].Value.ToString()); //把当前选中行的数据存入list数组中
}

for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
dataGridView1.Rows[rowIndex].Cells[j].Value = dataGridView1.Rows[rowIndex + 1].Cells[j].Value;
dataGridView1.Rows[rowIndex + 1].Cells[j].Value = list[j].ToString();
}
dataGridView1.Rows[rowIndex + 1].Selected = true;
dataGridView1.Rows[rowIndex].Selected = false;
}

//http://blog.csdn.net/wxm3630478/archive/2009/06/10/4256851.aspx
JOCLI 2010-03-19
  • 打赏
  • 举报
回复
用bindingsource吧
给你段代码,保证你解决问题

BindingSource bs = new BindingSource();
private void ProgramList_Load(object sender, EventArgs e)
{
SqlConnection con = DBCON.SqlConnettion();
string sql="select * from SystemUpdateHis";
SqlDataAdapter sdp = new SqlDataAdapter(sql, con);
con.Open();
DataSet ds = new DataSet();
sdp.Fill(ds, "SystemUpdateHis");
bs.DataSource = ds;
bs.DataMember = "SystemUpdateHis";
con.Close();
dataGridView1.DataSource = bs;
}

private void 下一笔_Click(object sender, EventArgs e)
{
bs.MoveNext();
}

private void 上一笔_Click(object sender, EventArgs e)
{
bs.MovePrevious();
}
zl4460072 2010-03-19
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 wxm3630478 的回复:]
哇 100分 要发个
http://blog.csdn.net/wxm3630478/archive/2009/06/10/4256851.aspx
[/Quote]
这个链接打开了 看不到的阿
zl4460072 2010-03-19
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 wxm3630478 的回复:]
哇 100分 要发个
http://blog.csdn.net/wxm3630478/archive/2009/06/10/4256851.aspx
[/Quote]
话都不说 直接来个链接阿?
wxm3630478 2010-03-19
  • 打赏
  • 举报
回复
zl4460072 2010-03-19
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 criedshy 的回复:]
引用 3 楼 zl4460072 的回复:
我只是想让显示的数据上下移动,而不是数据库里的数据移动,可以实现吗?


可以实现啊
[/Quote]
dataGridView1.Rows.Insert( index + 1, dr );

报错:无法想绑定的datagridview添加数据。。。。
zl4460072 2010-03-19
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 criedshy 的回复:]
引用 3 楼 zl4460072 的回复:
我只是想让显示的数据上下移动,而不是数据库里的数据移动,可以实现吗?


可以实现啊
[/Quote]
你的这个代码实现的是让所选中的行移动吧?
我请教的是行里的数据移动诶,还请再劳驾想象办法哦
zl4460072 2010-03-19
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 criedshy 的回复:]
引用 3 楼 zl4460072 的回复:
我只是想让显示的数据上下移动,而不是数据库里的数据移动,可以实现吗?


可以实现啊
[/Quote]
就这5个字阿。。。
小弟不是来请赐教了嘛。。。。。。
criedshy 2010-03-19
  • 打赏
  • 举报
回复
//下移一行
private void button2_Click( object sender, EventArgs e )
{

DataGridViewRow dr = dataGridView1.CurrentRow;
if ( dr.Index < dataGridView1.Rows.Count - 1 )
{
int index = dr.Index;
dataGridView1.Rows.Remove( dr );
dataGridView1.Rows.Insert( index + 1, dr );

dr.Selected = true;
dataGridView1.CurrentCell = dr.Cells[0];
}
}

//上移一行
private void button3_Click( object sender, EventArgs e )
{

DataGridViewRow dr = dataGridView1.CurrentRow;

if ( dr.Index > 0 )
{
int index = dr.Index;
dataGridView1.Rows.Remove( dr );
dataGridView1.Rows.Insert( index - 1, dr );

dr.Selected = true;
dataGridView1.CurrentCell = dr.Cells[0];
}
}

//首行
private void button4_Click( object sender, EventArgs e )
{
if ( dataGridView1.CurrentRow.Index != 0 )
{
DataGridViewRow dr = dataGridView1.CurrentRow;
dataGridView1.Rows.Remove( dr );
dataGridView1.Rows.Insert(0, dr );

dr.Selected = true;
dataGridView1.CurrentCell = dr.Cells[0];
}
}

//末行
private void button5_Click( object sender, EventArgs e )
{
if ( dataGridView1.CurrentRow.Index <dataGridView1.Rows.Count-1 )
{
DataGridViewRow dr = dataGridView1.CurrentRow;
dataGridView1.Rows.Remove( dr );
dataGridView1.Rows.Insert( dataGridView1.Rows.Count , dr );

dr.Selected = true;
dataGridView1.CurrentCell = dr.Cells[0];
}
}



criedshy 2010-03-19
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zl4460072 的回复:]
我只是想让显示的数据上下移动,而不是数据库里的数据移动,可以实现吗?
[/Quote]

可以实现啊
zl4460072 2010-03-19
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 changling_wang 的回复:]
换里面的数据就可以了~!
[/Quote]
怎么个换法呀??
zl4460072 2010-03-19
  • 打赏
  • 举报
回复
我只是想让显示的数据上下移动,而不是数据库里的数据移动,可以实现吗?
Return门徒 2010-03-19
  • 打赏
  • 举报
回复
你想把控件的布局方式改变~!太麻烦了吧~!换里面的数据就可以了~!
Return门徒 2010-03-19
  • 打赏
  • 举报
回复
换里面的数据就可以了~!

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧