Voldy

Hi, I have a datagridview control on a form that contains lots of rows populated from an SQL database (done in code in a grid refresh subroutine).

I have a timer control that updates the grid every 10 secs.

The problem I have is that if I am in the process of scrolling down the grid and the timer event fires off a grid refresh, the grid resets the grid location and scrollbars.

What I want to be able to do is set a flag on a mousedown event of the scrollbar and more importantly reset the flag on the mouseup event. This will enable the grid refresh routine to decide whether to refresh or not.

However I don't seem to be able to do this as the events do not exist. The Scroll event allows me to set the flag to disable the refresh but I don't seem to be able to find a similar event to allow me to enable the refresh again.

Can anyone help I am using VB2005.net

Cheers



Re: Windows Forms Data Controls and Databinding DataGridView Scrollbar

Bob zhu - MSFT

you can get scroll bar as an control from the datagridview if it exist, and add event for the scroll bar

it can determin whether your mouse is on the scrollbar and so on

private void form_load(object sender, EventArgs e)

{

foreach (Control c in this.dataGridView8.Controls)

{

((System.Windows.Forms.ScrollBar)c).MouseEnter += new EventHandler(Form1_MouseEnter);

}

}

void Form1_MouseEnter(object sender, EventArgs e)

{

MessageBox.Show("can not re fill");

}






Re: Windows Forms Data Controls and Databinding DataGridView Scrollbar

Zhi-Xin Ye - MSFT

The DataGridView has two controls in its control collection, the HScrollBar and VScrollBar,i.e. dataGridView1.Controls[0] is a HScrollBar object and dataGridView1.controls[1] is a VScrollBar object, handle the scrollbar's scroll event and MouseLeave event to stop or start the timer. Have a look at my sample write for you:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace Sample4

{

public partial class DgvScroll : Form

{

public DgvScroll()

{

InitializeComponent();

}

Timer timer;

DataTable dt;

private void DgvScroll_Load(object sender, EventArgs e)

{

dt = new DataTable();

dt.Columns.Add("id", typeof(int));

dt.Columns.Add("name");

dt.Columns.Add("aa");

for (int j = 0; j < 25; j++)

{

dt.Rows.Add(j, "Row " + j.ToString(), "aa");

}

this.dataGridView1.DataSource = dt;

//HScrollBar hScroll = this.dataGridView1.Controls[0] as HScrollBar;

VScrollBar vScroll = this.dataGridView1.Controls[1] as VScrollBar;

vScroll.Scroll += new ScrollEventHandler(vScroll_Scroll);

vScroll.MouseLeave += new EventHandler(vScroll_MouseLeave);

timer = new Timer();

timer.Interval = 10000;

timer.Tick += new EventHandler(timer_Tick);

timer.Start();

}

void vScroll_MouseLeave(object sender, EventArgs e)

{

timer.Start();

}

void timer_Tick(object sender, EventArgs e)

{

this.dataGridView1.DataSource = null;

this.dataGridView1.DataSource = dt;

}

void vScroll_Scroll(object sender, ScrollEventArgs e)

{

timer.Stop();

}

}

}


Hope it helps.
Best Regards.
Zhixin Ye




Re: Windows Forms Data Controls and Databinding DataGridView Scrollbar

Jez Heaton

Hi Guys and thanks for the replies.

I have tried to implement what you have suggested but as I am not a C programmer I am not having much joy. Any chance you could provide a Visual Basic version

Zhixin, I do not seem to be able to get the events to trigger. Below is what I have done so far but I have been unable to define the events:

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

SuspendRefresh = False

RefreshGrid()

Dim VScroll As VScrollBar = DataGridView1.Controls(1)

'need to add event stuff here

'C code provided (to be converted)

'VScrollBar vScroll = this.dataGridView1.Controls[1] as VScrollBar;

'vScroll.Scroll += new ScrollEventHandler(vScroll_Scroll);

'vScroll.MouseLeave += new EventHandler(vScroll_MouseLeave);

End Sub

Sub VScroll_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.VScrollBar)

'not sure if the args are correct - these routines do not seem to trigger

SuspendRefresh = True

End Sub

Sub VScroll_MouseLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.VScrollBar)

SuspendRefresh = False

End Sub





Re: Windows Forms Data Controls and Databinding DataGridView Scrollbar

Zhi-Xin Ye - MSFT

Ok, VB guys, I translate my codes into VB.NET for you:

Public Class DgvScroll

Inherits Form

Private timer As Timer

Private dt As DataTable

Public Sub New()

MyBase.New()

InitializeComponent()

End Sub

Private Sub DgvScroll_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load

dt = New DataTable

dt.Columns.Add("id", GetType(System.Int32))

dt.Columns.Add("name")

dt.Columns.Add("aa")

Dim j As Integer = 0

Do While (j < 25)

dt.Rows.Add(j, ("Row " + j.ToString), "aa")

j = (j + 1)

Loop

Me.DataGridView1.DataSource = dt

'HScrollBar hScroll = this.dataGridView1.Controls[0] as HScrollBar;

Dim vScroll As VScrollBar = CType(Me.DataGridView1.Controls(1), VScrollBar)

AddHandler vScroll.Scroll, AddressOf Me.vScroll_Scroll

AddHandler vScroll.MouseLeave, AddressOf Me.vScroll_MouseLeave

timer = New Timer

timer.Interval = 3000

AddHandler timer.Tick, AddressOf Me.timer_Tick

timer.Start()

End Sub

Private Sub vScroll_MouseLeave(ByVal sender As Object, ByVal e As EventArgs)

timer.Start()

End Sub

Private Sub timer_Tick(ByVal sender As Object, ByVal e As EventArgs)

Me.dataGridView1.DataSource = Nothing

Me.dataGridView1.DataSource = dt

End Sub

Private Sub vScroll_Scroll(ByVal sender As Object, ByVal e As ScrollEventArgs)

timer.Stop()

End Sub

End Class






Re: Windows Forms Data Controls and Databinding DataGridView Scrollbar

Jez Heaton

Fantastic! Thank you very much Zhi

Works perfectly