如何在MouseUp事件而不是MouseDown事件中获取DataGridView列的索引? [英] How to get a DataGridView column's index in a MouseUp event, not in a MouseDown event?

查看:118
本文介绍了如何在MouseUp事件而不是MouseDown事件中获取DataGridView列的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 DataGridView 的WinForm,我的目标是将一个列拖放到另一列索引上。我知道可以通过使用 AllowUserToOrderColumns = true 来对列进行重新排序。但是我必须对DGV执行其他操作。这就是为什么在鼠标向上移动时需要目标列索引的原因。为此,我使用 HitTestInfo

I have a WinForm with DataGridView, my goal is to drag one column and drop it on other column index. I know column reordering is possible by using AllowUserToOrderColumns = true. But I have to perform other operations on DGV. That's why I need the target column index at a mouse-up event. To do that, I use HitTestInfo:

System.Windows.Forms.DataGrid.HitTestInfo myHitTest;
myHitTest = dataGrid1.HitTest(e.X, e.Y);
int p = myHitTest.ColumnIndex;

当我单击第一个DGV列时,此代码运行并为我提供该列的索引( p )。问题是当我将其放在DGV的另一列时,我想知道目标列的索引,并且使用相同的代码 p = -1 ,我认为是因为 HitTestInfo 成员在<$ c $ MouseDown 上返回值,在<$ c $上返回 not c> MouseUp 。如果有人可以告诉我该怎么做,那就太好了。

When I click on the first DGV column, this code runs and gives me the column's index (p). The problem is when I drop it on the other column of DGV, I'd like to know the target column's index, with the same code p = -1, I think because the HitTestInfo member returns a value on a MouseDown and not on a MouseUp. If anyone can tell me how to do it, it would be very great.

推荐答案

您可以创建两个HitTestInfo对象,其中一个 MouseDown 和一个

You can create two HitTestInfo objects, one in the MouseDown and one in the MouseUp.

IMO ,您还应该使用 DataGridView.HitTestInfo 类,而不是 DataGrid.HitTestInfo 类,并尝试不调用或命名 DataGridViews DataGrids ,它是与 WPF 类似但不同的控件!

IMO, you also should use the DataGridView.HitTestInfo class, not DataGrid.HitTestInfo and try to not call or name DataGridViews DataGrids, which is a similar but different Control from WPF!

DataGridView.HitTestInfo myHitTestDown, myHitTestUp;
int visibleColumnDown, visibleColumnUp;

private void dataGrid1_MouseUp(object sender, MouseEventArgs e)
{
    myHitTestUp = dataGrid1.HitTest(e.X, e.Y);
    visibleColumnUp = getVisibleColumn(dataGrid1, e.X);
}

private void dataGrid1_MouseDown(object sender, MouseEventArgs e)
{
    myHitTestDown = dataGrid1.HitTest(e.X, e.Y);
    visibleColumnDown = getVisibleColumn(dataGrid1, e.X);
}

更新:查找a的可见索引

dataGrid1.Columns[myHitTestUp.ColumnIndex].DisplayIndex;

在我发现之前,我写了这个小辅助函数,其功能相同:

Before I found that, I wrote this little helper function, which does the same:

int getVisibleColumn(DataGridView dgv, int x)
{
    int cx = dgv.RowHeadersWidth;
    int c = 0;
    foreach (DataGridViewColumn col in dgv.Columns)
    {
        cx += col.Width; if ( cx >= x) return c; c++;
    }
    return -1;
}

要弄清哪一栏被改组似乎有些困难。有一个事件,该事件被受影响的列中的每个列所调用,并且始终会被拖动的那个事件首先调用。这是一种实现方法:

To find out which Column was shuffled seems to be a bit harder. There is an event, which gets called for each column that was affected and it always gets called first for the one that was dragged along. Here is one way to do it:

在类级别创建变量:

List<DataGridViewColumn> shuffled = new List<DataGridViewColumn>();
DataGridViewColumn shuffledColumn = null;

记住第一列:

private void dgvLoadTable_ColumnDisplayIndexChanged(
             object sender, DataGridViewColumnEventArgs e)
{
    if (shuffledColumn == null) shuffledColumn = e.Column;
}

忘记之前发生的事情:

private void dgvLoadTable_MouseDown(object sender, MouseEventArgs e)
{  
    shuffledColumn = null;
}

现在您可以使用它了。但是,选择列不能很好地进行改组!如果这样做

Now you can use it. Selecting Columns is, however, not going well with shuffling them! If you do

 shuffledColumn.Selected = true;

只有在 SelectionMode 时才会选择是 FullColumnSelect ColumnHeaderSelect -恐怕在这两种模式下改组都无法工作。.

it will only be selected if the SelectionMode is either FullColumnSelector ColumnHeaderSelect- In either mode the shuffling will not work, I'm afraid..

这篇关于如何在MouseUp事件而不是MouseDown事件中获取DataGridView列的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆