【问题标题】:How to know whether the user is scrolling the datagridview如何知道用户是否正在滚动datagridview
【发布时间】:2010-10-05 15:11:41
【问题描述】:

我想知道用户是否正在滚动 DataGridView。

当用户滚动 DataGridView 时,我希望暂停正在运行的线程并在用户停止滚动后立即恢复该线程。

任何帮助都将深表感谢。

非常感谢:)

更新

对于我的工作,代码在这里:- Updating DataGridView via a thread when scrolling

【问题讨论】:

  • +1 到目前为止,我从来不需要对滚动事件采取行动,但如果有一天我这样做了,鉴于其答案,您的问题可能会很有用。

标签: c# multithreading datagridview


【解决方案1】:

请看这里,这是一个使用 ListView 的示例,但它可以很容易地适应 DataGridView。

ListView onScroll event

【讨论】:

  • 感谢您的回答。我赞成你的回答。但我是一个新手编码员,无法在哪里添加此代码,请帮助......这是我的代码:-stackoverflow.com/questions/3766784/…。请让我知道在哪里实现此代码,如果你能提供demo就好了......
【解决方案2】:
public class DataGridViewEx : DataGridView
    {
        private const int WM_HSCROLL = 0x0114;
        private const int WM_VSCROLL = 0x0115;
        private const int WM_MOUSEWHEEL = 0x020A;

        public event ScrollEventHandler ScrollEvent;
        const int SB_HORZ = 0;
        const int SB_VERT = 1;
        public int ScrollValue;
        [DllImport("User32.dll")]
        static extern int GetScrollPos(IntPtr hWnd, int nBar);
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == WM_VSCROLL ||
                m.Msg == WM_MOUSEWHEEL)
                if (ScrollEvent != null)
                {
                    this.ScrollValue = GetScrollPos(Handle, SB_VERT);
                    ScrollEventArgs e = new ScrollEventArgs(ScrollEventType.ThumbTrack, ScrollValue);
                    this.ScrollEvent(this, e);
                }            
        }
    }

将暂停代码添加到 ScrollEvent 事件的处理程序

【讨论】:

  • 感谢您的回答。我赞成你的回答。但我是一名新手编码员,无法在哪里添加此代码,请帮助......这是我的代码:-stackoverflow.com/questions/3766784/…。请让我知道在哪里实现此代码,如果你能提供demo就好了......
  • 你需要在你的项目中创建新的类并将我的代码 sn-p 粘贴到它上面。然后在您的表单设计器中改为 DataGridView datagrid = new DataGridView();你必须写下一个:DataGridView datagrid = new DataGridViewEx();
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多