2
\$\begingroup\$

I have a DataGridView whose Virtual Mode property is set to True. When values of the underlying list are updated, those updates are not occurring in the UI unless I scroll vertically.

So I have created a timer which, every 1 second, calls DataGridView.Refresh(). Feels ineffective and overkill. Is there a better way, taking into consideration the current design of my application?

public partial class ProxyTesterView : UserControl
{
    public List<Proxy> Proxies = new List<Proxy>();
    public List<ProxyJudge> Judges = new List<ProxyJudge>();
    public List<ProxyTest> Tests = new List<ProxyTest>();

    private PauseOrCancelTokenSource pcts = new PauseOrCancelTokenSource();

    public ProxyTesterView()
    {
        InitializeComponent();

        //Fill lists with testing data for debugging purposes.
        for (int i = 0; i < 10000; i++)
        {
            Proxies.Add(new Proxy("127.0.0." + RandomUtility.GetRandomInt(1, 5), 8888));
        }
        ProxiesDataGridView.RowCount = Proxies.Count;

        Judges.Add(new ProxyJudge("http://azenv.net"));
    }

    private void ValidateProxiesButton_Click(object sender, EventArgs e)
    {
        ValidateProxiesMenu.Show(ValidateProxiesButton, new Point(0, ValidateProxiesButton.Height));
    }

    private void AddProxiesButton_Click(object sender, EventArgs e)
    {
        AddProxiesMenu.Show(AddProxiesButton, new Point(0, AddProxiesButton.Height));
    }

    private void RemoveProxiesButton_Click(object sender, EventArgs e)
    {
        RemoveProxiesMenu.Show(RemoveProxiesButton, new Point(0, RemoveProxiesButton.Height));
    }

    private void StartValidatingProxiesMenuItem_Click(object sender, EventArgs e)
    {
        UpdateProxyDGVTimer.Start();
        Task.Run(async () =>
        {
            await ProxyTester.Start(Proxies, Judges, Tests, pcts.Token, 1);
        });
    }

    private void PauseValidatingProxiesMenuItem_Click(object sender, EventArgs e)
    {
        pcts.Pause();
    }

    private void ResumeValidatingProxiesMenuItem_Click(object sender, EventArgs e)
    {
        pcts.Resume();
    }

    private void StopValidatingProxiesMenuItem_Click(object sender, EventArgs e)
    {
        pcts.Cancel();
    }

    private void ProxiesDataGridView_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
    {
        var proxy = Proxies[e.RowIndex];
        switch (e.ColumnIndex)
        {
            case 0:
                e.Value = proxy.IP;
                break;
            case 1:
                e.Value = proxy.Port;
                break;
            case 2:
                e.Value = proxy.IsValid;
                break;
            default:
                break;
        }
    }

    private void UpdateProxyDGVTimer_Tick(object sender, EventArgs e)
    {
        Console.WriteLine("Timer");
        ProxiesDataGridView.Refresh();
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.