C#中datagridview能否实现拖拽(dragdrop)行实现行的排序功能?

nishizhen 2009-09-24 08:32:23
如题。

...全文
1068 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
movine 2011-07-19
  • 打赏
  • 举报
回复
刚好要这个功能,看看能否满足
lzsh0622 2009-09-24
  • 打赏
  • 举报
回复
用上面的方法,变一下操作对象 DataGridViewRow --> DataRow 这种实现方法没问题.
nishizhen 2009-09-24
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 lzsh0622 的回复:]
呵呵,本人原创.实现了拖拽当前行的基本功能。
如果有时间,在此基础上,还可以发挥写出好多效果来。
C# codeDataGridViewRow row=null;privatevoid dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{if (e.ColumnIndex==-1&& e.RowIndex!=-1)
{
row= ((DataGridView)sender).Rows[e.RowIndex];
}
}privatevoid dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{if (e.ColumnIndex==-1&& e.RowIndex!=-1&& row!=null&& row.Index!= e.RowIndex)
{bool down= (e.RowIndex> row.Index+1);
((DataGridView)sender).Rows.Remove(row);
((DataGridView)sender).Rows.Insert(((down)? e.RowIndex-1 : e.RowIndex), row);
((DataGridView)sender).CurrentCell= ((DataGridView)sender)[0, (down)? e.RowIndex-1 : e.RowIndex];
row=null;
}
}
[/Quote]

我试了一下,这样在运行 ((DataGridView)sender).Rows.Insert(((down)? e.RowIndex-1 : e.RowIndex), row);
这句的时候 会报错“Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.“

也就是说绑定数据的不可以增加列,这怎么办?

我转接到一个DataTable再做?
lzsh0622 2009-09-24
  • 打赏
  • 举报
回复
呵呵,本人原创.实现了拖拽当前行的基本功能。
如果有时间,在此基础上,还可以发挥写出好多效果来。
DataGridViewRow row = null;
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.ColumnIndex == -1 && e.RowIndex != -1)
{
row = ((DataGridView)sender).Rows[e.RowIndex];
}
}

private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.ColumnIndex == -1 && e.RowIndex != -1 && row != null && row.Index != e.RowIndex)
{
bool down = (e.RowIndex > row.Index + 1);
((DataGridView)sender).Rows.Remove(row);
((DataGridView)sender).Rows.Insert(((down) ? e.RowIndex - 1 : e.RowIndex), row);
((DataGridView)sender).CurrentCell = ((DataGridView)sender)[0, (down) ? e.RowIndex - 1 : e.RowIndex];
row = null;
}
}
yidichaxiang 2009-09-24
  • 打赏
  • 举报
回复
mark
s32702 2009-09-24
  • 打赏
  • 举报
回复
学习一下
nishizhen 2009-09-24
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 lzsh0622 的回复:]
稍侯
[/Quote]

谢谢啊~

非常感谢
lzsh0622 2009-09-24
  • 打赏
  • 举报
回复
稍侯
nishizhen 2009-09-24
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 lzsh0622 的回复:]
可以实现,但是拖拽与多选按键冲突,分不清是多选还是拖拽,只能限制执行一种。

[/Quote]

怎么实现啊???

我不要多选。

谢谢
lzsh0622 2009-09-24
  • 打赏
  • 举报
回复
可以实现,但是拖拽与多选按键冲突,分不清是多选还是拖拽,只能限制执行一种。
nishizhen 2009-09-24
  • 打赏
  • 举报
回复
???

我想到一个思路就是 在一列里面 放置一个什么能够拖拽的控件,然后用它来识别排序.

是否可行?
nishizhen 2009-09-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 sito_hongta 的回复:]
MS沒有這樣的功能,幫你頂!
[/Quote]
是啊,好像只有整个的DataGridView的dragdrop方法,

没有Cell 或者 Row的。。。

谢谢啊~
顶,等待中。。。
zcandyly20211 2009-09-24
  • 打赏
  • 举报
回复
up
sito_hongta 2009-09-24
  • 打赏
  • 举报
回复
MS沒有這樣的功能,幫你頂!
nishizhen 2009-09-24
  • 打赏
  • 举报
回复
我没用上。。。

最后采用了别的方法~

110,502

社区成员

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

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

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