C# 使用c将事件连接到数据网格视图中的垂直滚动条上箭头和下箭头#
我有一个像这样的数据网格视图…在下图中,这很好用 我需要在垂直侧栏上挂起一个事件 我的意思是,如果我点击滚动条上的上箭头,我想做点什么 如果我点击滚动条上的向下箭头,我想做点什么 更具体地说,当我点击垂直滚动条上的上箭头时,我想得到第一条上记录的id 我该怎么做,我正在使用winforms 有人能帮我吗 非常感谢 我发现了这一点,但我不知道如何在我的页面中实现这一点C# 使用c将事件连接到数据网格视图中的垂直滚动条上箭头和下箭头#,c#,.net,winforms,datagridview,vertical-scrolling,C#,.net,Winforms,Datagridview,Vertical Scrolling,我有一个像这样的数据网格视图…在下图中,这很好用 我需要在垂直侧栏上挂起一个事件 我的意思是,如果我点击滚动条上的上箭头,我想做点什么 如果我点击滚动条上的向下箭头,我想做点什么 更具体地说,当我点击垂直滚动条上的上箭头时,我想得到第一条上记录的id 我该怎么做,我正在使用winforms 有人能帮我吗 非常感谢 我发现了这一点,但我不知道如何在我的页面中实现这一点 using System.Reflection; using System.Windows.Forms; bool addS
using System.Reflection;
using System.Windows.Forms;
bool addScrollListener(DataGridView dgv)
{
bool ret = false;
Type t = dgv.GetType();
PropertyInfo pi = t.GetProperty("VerticalScrollBar", BindingFlags.Instance | BindingFlags.NonPublic);
ScrollBar s = null;
if (pi != null)
s = pi.GetValue(dgv, null) as ScrollBar;
if (s != null)
{
s.Scroll += new ScrollEventHandler(s_Scroll);
ret = true;
}
return ret;
}
void s_Scroll(object sender, ScrollEventArgs e)
{
// Hander goes here..
}
我已经这样做了
private void s_Scroll(object sender, ScrollEventArgs e)
{
if (e.ScrollOrientation == ScrollOrientation.VerticalScroll)
{
if (e.Type == ScrollEventType.ThumbPosition)
{
if (e.Type == ScrollEventType.SmallIncrement)
{
int i = dgvMembers.FirstDisplayedScrollingRowIndex;
int idemebers =Convert.ToInt32(dgvMembers.Rows[i].Cells["Id"].Value.ToString());
getMemberInfo(i, idemebers);
}
if (e.Type == ScrollEventType.SmallDecrement)
{
int i = dgvMembers.FirstDisplayedScrollingRowIndex;
int idemebers = Convert.ToInt32(dgvMembers.Rows[i].Cells["Id"].Value.ToString());
getMemberInfo(i, idemebers);
}
}
}
}
但此事件不会激发s.Scroll+=新的ScrollEventHandler(s_Scroll);
它不会进入这个事件
请任何人帮帮忙……你应该能够使用你发布的代码。您只需在某个地方调用
addScrollListener
(例如在InitializeComponent
之后的构造函数中)
您可能会创建自己的自定义滚动条。。。我认为Windows不会暴露这些类型的事件。@SpikeX我正在使用上述代码,但不知道如何启动……这些代码不起作用。这是一个当整个网格滚动时的事件,而不是当你特别单击一个向上或向下箭头时。你是否可以查看我修改的代码?事件不会触发。@SpikeX请查看我修改的问题他希望在你向上滚动和向下滚动时有单独的事件,因此,仅仅添加一个滚动侦听器并不能满足他的需要。
public Form1()
{
InitializeComponent();
// Replace dataGridView1 with the name of your DataGridView
addScrollListener(dataGridView1);
}
// addScrollListener code goes here