Mouse Wheel Scrolling Causes DataGrid To Loose Focus

B

Bryan Masephol

Hi All

I got a datagrid and a ComboBox on a form. I populate the combobox with
years for all the data avaiable. When the user chooses a year the datagrid
is populated with the specific years information.

Problem... when the users scrolls the datagrid with the mouse wheel it work
for 2 wheel clicks and then for some reason the focus jumps to the combobox
and the year is then changes there for causing the data to change and errors
.... sometimes.

Anybody experience this or know how to fix it...

Thanks
Bryan
 
B

Bryan Masephol

Hmmm

Thanks for the input Alex but it seemed to make things worse. Even though I
select the datagrid when the mouse enters it still wants to jump to my
combobox. Very weird. I'll keep trying things and post what I find.

If anyone else had problems like this please contribute.

Bryan
 
A

AlexS

I had something similar with textbox and datagrid on same panel.
I used MouseEnter event to select datagrid

myDataGrid.Select();

For me it did the trick. How's it for you?

Note, that I was updating content of grid on mouse wheel. It might've been
related to dynamic change of data there.

HTH
Alex
 
A

AlexS

It looks like your combo is somehow linked to grid - or uses maybe same
datasource?

I had textbox, which was filled with data from datagrid. However, I did
filling myself - did not use any internal links, like datasource,
databinding etc. If focus is jumping to combo - there is some code which
does it. While mouse is hovering over selected grid combo should not get any
input.

HTH
Alex
 
R

Russ Perry Jr

I normally wouldn't reply to posts this old, but they seem really
similar
to what I'm working on...

Bryan Masephol said:
I got a datagrid and a ComboBox on a form. I populate the combobox
with years for all the data avaiable. When the user chooses a year
the datagrid is populated with the specific years information.

Problem... when the users scrolls the datagrid with the mouse wheel
it work for 2 wheel clicks and then for some reason the focus jumps
to the combobox and the year is then changes there for causing the
data to change and errors ... sometimes.

I have a similar setup, though I have TWO datagrids. If I click in
the bottom grid, then just like happened to Bryan, after 2 wheel
clicks the focus shifts to the ComboBox. If I click in the top grid,
then after 2 clicks, focus moves to the lower grid, then to the
ComboBox on the next click.

AlexS replied to Bryan:
I had something similar with textbox and datagrid on same panel.
I used MouseEnter event to select datagrid

myDataGrid.Select();

For me it did the trick. How's it for you?

It's unclear what component's event I'm supposed to do that on, but
doing it on the grid does squat for me.

AlexS later suggested that perhaps Bryan had a DataSource being
shared between the ComboBox and DataGrid, but that doesn't seem to
be the case in my application (the ComboBox always gets a new
DataSource). But it wasn't clear if AlexS meant literally the
DataSouce component or a more general "data source" and possibly
therefore meaning something else...
Does anyone else have any (other) ideas/solutions?
 
R

Russ Perry Jr

I have a similar setup, though I have TWO datagrids. If I click in
the bottom grid, then just like happened to Bryan, after 2 wheel
clicks the focus shifts to the ComboBox. If I click in the top grid,
then after 2 clicks, focus moves to the lower grid, then to the
ComboBox on the next click.

I hate following up my own posts, but I just noticed something --
it's not "2 wheel clicks" before it changes focus, but rather it's
when the selected row scrolls out of the "viewport".

Also, it's changing focus to the next component in TabOrder; I set
TabStop to false for the two DataGrids and the ComboBox and now
focus jumps to a TextBox (which I'd like to keep tab-able along
with the other components around it) when the selected row scrolls
out of the DataGrid's viewable area.

I have some code to select the whole row instead of just the cell
clicked in, as shown below -- is there maybe something to this
that's screwing things up? I don't think so, since I commented
that out of the DataGrids' MouseUp events, but who knows.

Any ideas?

private void DataGrid_SelectFullRow(DataGrid grid,
System.Windows.Forms.MouseEventArgs e)
{
System.Drawing.Point pt = new Point(e.X, e.Y);
DataGrid.HitTestInfo hti = grid.HitTest(pt);

if (hti.Type == DataGrid.HitTestType.Cell)
{
grid.CurrentCell = new DataGridCell(hti.Row, hti.Column);
grid.Select(hti.Row);
}
}
 
R

Russ Perry Jr

Solved... For posterity:
it's not "2 wheel clicks" before it changes focus, but rather it's
when the selected row scrolls out of the "viewport".

After two days, I finally managed to come up with the right search
criteria to find an answer...

From: http://www.dotnetforums.net/t78467.html
Private Sub DataGrid1_MouseWheel(ByVal sender As Object,
ByVal e As System.Windows.Forms.MouseEventArgs)
Handles DataGrid1.MouseWheel
DataGrid1.Focus()
End Sub

I'm using C#, but the idea was clear enough, and I got it working.

....Though Visual Studio (7.1.3088 with .Net 1.1.4322) doesn't seem
to give me the MouseWheel event in the visual builder, so I had to
add it manually to the "Windows Form Designer generated code",
which always makes me a little nervous since it says "do not modify
the contents of this method with the code editor". I don't know if
Visual Studio is set up incorrectly, or if later versions list this
event, or what, but ugh...

In that section:

this.dataGridComponent.MouseWheel += new
System.Windows.Forms.MouseEventHandler(this.DataGrid_MouseWheel);

....and elsewhere:

private void DataGrid_MouseWheel(object sender,
System.Windows.Forms.MouseEventArgs e)
{
((DataGrid) sender).Focus();
}

....and no scrolling with the mouse wheel works.
 

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