在DataGridView上移动行时的可视标记 [英] Visual marker when moving rows on DataGridView
问题描述
用户在DataGridView中上下拖动行。我有拖动逻辑,但我希望有一个黑色的标记,表示我放弃鼠标后将放置的行。
Users drag rows up and down in my DataGridView. I have the dragging logic down-pat, but I'd like there to be a dark marker indicating where the row will be placed after I let go of the mouse.
Microsoft Access的示例http://img718.imageshack.us/img718/8171/accessdrag。 png
来自Microsoft Access的示例;我想拖动列而不是列
有谁知道我会怎么做?这是内置的,还是我必须绘制自己的标记(如果是,我该怎么做)?
Does anyone know how I'd go about doing this? Is this built-in, or would I have to draw my own marker (if so, how do I do that)?
谢谢!
推荐答案
几年前我做了一个树视图;不记得如何,但是请考虑使用DataGridView的 MouseMove
事件。
I did this for a treeview a couple years ago; can't remember exactly how, but consider using the MouseMove
event of the DataGridView.
当拖动发生时,您的MouseMove处理程序应该:
While the drag is occurring, your MouseMove handler should:
- 获取
鼠标的相对坐标(MouseEventArgs包含
的坐标,但我认为它们是屏幕坐标,因此您可以使用DataGridView.PointToClient()
将它们转换为相对位置) - 确定哪个行在那个X
位置(有没有一个方法?如果不是,可以通过将行+行头高度相加来计算,但请记住,网格可能已被滚动) - 突出显示该行或使其
边框变暗。您可能可以使一个边框变暗的一种方法是通过更改DataGridViewRow.DividerHeight
属性。 - 当鼠标移到
行,恢复它以前的
看起来。
- get the relative coordinates of the
mouse (the MouseEventArgs contains
the coordinates, but I think they're screen coordinates, so you can use
DataGridView.PointToClient()
to convert them to relative) - determine which row is at that X position (is there a method for this? If not, you can calculate it by adding up the row + row header heights, but remember that the grid may have been scrolled)
- highlight that row or darken its
border. One way you may be able to darken one border is by changing the
DataGridViewRow.DividerHeight
property. - when the mouse moves outside that row, restore it to how it previously looked.
如果你想做一些自定义的外观(而不是仅使用可用的属性),您可以使用 DataGridView.RowPostPaint
事件。如果您为此事件实现一个处理程序,该处理程序仅在将行拖动到另一行上时使用,则可以使用更大笔刷重新绘制行的顶部或底部边框。 这里的MSDN示例
If you wanted to do something custom with the appearance of the row under the mouse (instead of just using the available properties), you can use the DataGridView.RowPostPaint
event. If you implement a handler for this event which is only used when a row is being dragged over another row, you can repaint the top or bottom border of the row with a bolder brush. MSDN example here.
这篇关于在DataGridView上移动行时的可视标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!