上移动的DataGridView行时视觉标记

上移动的DataGridView行时视觉标记

本文介绍了上移动的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实例;我想拖,而不是列的行


Example from Microsoft Access; I want to drag rows instead of columns

有谁知道我怎么会去这样做?这是内置的,或者我会画我自己的标志(如果是这样,我怎么做)?

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 属性。
  • 当鼠标之外的移动行,其恢复到它如何previously看着。
  • get the relative coordinates of themouse (the MouseEventArgs containsthe 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 Xposition (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 itsborder. One way you may be able to darken one border is by changing the DataGridViewRow.DividerHeight property.
  • when the mouse moves outside thatrow, restore it to how it previouslylooked.

如果你想做一些定制与行下(只使用可用的属性,而不是)的鼠标,你可以使用 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行时视觉标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 17:12