我有一个像这样的数据网格视图....在下面的图像中,这样可以正常工作......
我需要在垂直边栏中挂钩事件..
我的意思是如果我点击滚动条中的上箭头我想做点什么......
更具体一点,当我点击垂直滚动条中的上箭头时,我想得到第一个上记录id的id ..
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;
}
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); // i want to the details of selected record into text boxes
}
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 += new ScrollEventHandler(s_Scroll);
它没有进入这个事件...
任何人都可以帮忙...
非常感谢提前
答案 0 :(得分:2)
尝试使用DataGridView.Scroll事件。
更具体一点,当我点击垂直滚动条中的上箭头时,我想得到第一个上记录id的id
在DataGridView.Scroll
事件处理程序中,您可以执行此操作(上箭头被视为一个小的减量:
if (e.ScrollOrientation == ScrollOrientation.VerticalScroll
&& e.Type == ScrollEventType.SmallDecrement)
{
int i = dgvMembers.FirstDisplayedScrollingRowIndex;
// your code to process the first displayed row here
}