Workaround for scroll event in DataGrid?

T

Tim Wilson

The CFx DataGrid does not have a Scroll event. However, since the DataGrid
is custom drawn, the scrollbars are just added as fields to the DataGrid
object. Therefore, you can get access to those fields via reflection. Once
you get the scrollbars you can hook into the ValueChanged event of each and
this should let you know when the grid has been scrolled. This way of doing
it might not be perfect but if you really need something then this is an
option.

using System.Reflection;

FieldInfo[] fields =
this.dataGrid1.GetType().GetFields(BindingFlags.NonPublic |
BindingFlags.Instance);
foreach (FieldInfo field in fields)
{
object obj = field.GetValue(this.dataGrid1);
if (obj is VScrollBar)
{
((VScrollBar)obj).ValueChanged += new EventHandler(DataGrid_VScroll);
}
else if (obj is HScrollBar)
{
((HScrollBar)obj).ValueChanged += new EventHandler(DataGrid_HScroll);
}
}

private void DataGrid_VScroll(object sender, EventArgs e)
{
// Do something.
}

private void DataGrid_HScroll(object sender, EventArgs e)
{
// Do something.
}

Alternatively, you can go and get the fields themselves directly, based on
their declared names ("m_sbVert" and "m_sbHorz"). I just figured that the
way shown above should be more stable long term. If the internal names of
the scrollbars change then the above code should still work.
 
E

Earl

Thanks Tim, I'll play with that.

Tim Wilson said:
The CFx DataGrid does not have a Scroll event. However, since the DataGrid
is custom drawn, the scrollbars are just added as fields to the DataGrid
object. Therefore, you can get access to those fields via reflection. Once
you get the scrollbars you can hook into the ValueChanged event of each and
this should let you know when the grid has been scrolled. This way of doing
it might not be perfect but if you really need something then this is an
option.

using System.Reflection;

FieldInfo[] fields =
this.dataGrid1.GetType().GetFields(BindingFlags.NonPublic |
BindingFlags.Instance);
foreach (FieldInfo field in fields)
{
object obj = field.GetValue(this.dataGrid1);
if (obj is VScrollBar)
{
((VScrollBar)obj).ValueChanged += new EventHandler(DataGrid_VScroll);
}
else if (obj is HScrollBar)
{
((HScrollBar)obj).ValueChanged += new EventHandler(DataGrid_HScroll);
}
}

private void DataGrid_VScroll(object sender, EventArgs e)
{
// Do something.
}

private void DataGrid_HScroll(object sender, EventArgs e)
{
// Do something.
}

Alternatively, you can go and get the fields themselves directly, based on
their declared names ("m_sbVert" and "m_sbHorz"). I just figured that the
way shown above should be more stable long term. If the internal names of
the scrollbars change then the above code should still work.

--
Tim Wilson
.Net Compact Framework MVP

Earl said:
Is there a known method of implementing a scroll event in the CF Datagrid?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top