DataGridView更新/刷新问题

时间:2015-08-14 12:04:33

标签: c# datagridview task

我有一个Windows窗体,其中包含以下代码

BindingList<TicketResult> tickResults = new BindingList<TicketResult>();
BindingSource bindingSource1 = new BindingSource();
Action<String> call;

private void method(String x)
{
    if (this.dataGridView1.InvokeRequired)
    {
        lock (this)
        {                    
            dataGridView1.Invoke(
            new MethodInvoker(() =>
            {
                Debug.WriteLine(x);
                tickResults[int.Parse(x)].Row = "first page";
                dataGridView1.Refresh();
            }));
        }
    }
}

public Form1()
{
    call = method;

    ServicePointManager.DefaultConnectionLimit = 48;
    InitializeComponent();

    tickResults.ListChanged += tickResults_ListChanged;

    for (int i = 0; i < 10; i++)
    {
        TicketResult result = new TicketResult();                
        tickResults.Add(result);
    }

    bindingSource1.DataSource = tickResults;

    dataGridView1.DataSource = bindingSource1;


    for (int i = 0; i < 10; i++)
    {
        Search s = new Search();
        int x = i;
        Task.Run(() => s.start(x, this.call));
    }


}

我不明白为什么在没有调用tickResults dataGridView1's方法的情况下,Refresh()的更改未得到反映。

在表单中调用“call”委托的其他类的代码如下:

class Search : ISearch
{
    public async Task<bool> start(int i, Action<String> x)
    {
        bool result = false;
        TicketLogic tixLogic = new TicketLogic();

        try
        {
            await Task.Run(() => tixLogic.processFirstPage(i, x))
                .ContinueWith((t) => tixLogic.processSecondPage(i, x))
                .ContinueWith((t) => tixLogic.processThirdPage(i, x));               

            result = true;
        }
        catch (Exception e)
        {
            Debug.WriteLine(e.Message);
            result = false;
        }

        return result;
    }

    public async Task<bool> stop()
    {
        return false;
    }

    public async Task<bool> restart()
    {
        return false;
    }
}

class TicketLogic
{
    public async Task<bool> processFirstPage(int i, Action<String> x)
    {
        bool result = false;            

        try
        {
            HttpWebRequest request = WebRequest.CreateHttp("http://www.google.com");
            WebResponse response = await request.GetResponseAsync();
            StreamReader reader = new StreamReader(response.GetResponseStream());

            String textResponse = await reader.ReadToEndAsync();                

            reader.Close();
            response.Close();              

            result = true;
        }
        catch (Exception e)
        {
            Debug.WriteLine(e.Message);
            result = false;
        }

        return result;
    }

    public async Task<bool> processSecondPage(int i, Action<String> x)
    {
        bool result = false;            

        try
        {
            HttpWebRequest request = WebRequest.CreateHttp("http://www.example.com");
            WebResponse response = await request.GetResponseAsync();
            StreamReader reader = new StreamReader(response.GetResponseStream());

            String textResponse = await reader.ReadToEndAsync();

            //tixResult.Information = "Second Page";

            reader.Close();
            response.Close();

            x(i.ToString());

            result = true;
        }
        catch (Exception e)
        {
            Debug.WriteLine(e.Message);
            result = false;
        }

        return result;
    }

    public async Task<bool> processThirdPage(int i, Action<String> x)
    {
        bool result = false;

        try
        {
            HttpWebRequest request = WebRequest.CreateHttp("http://www.hotmail.com");
            WebResponse response = await request.GetResponseAsync();
            StreamReader reader = new StreamReader(response.GetResponseStream());

            String textResponse = await reader.ReadToEndAsync();

            //tixResult.Information = "Third Page";

            reader.Close();
            response.Close();

            x(i.ToString());

            result = true;
        }
        catch (Exception e)
        {
            Debug.WriteLine(e.Message);
            result = false;
        }
        return result;
    }
}

在此之前我尝试了另一种方法,其中我将数据绑定对象传递给计算任务,其中数据绑定对象被操纵,但即使在那里结果也是相同的,即对象中的更改未反映在网格,直到我点击网格中的一些单元格或最小化/最大化表格。

我的问题是,为什么在没有调用datagrid的情况下没有在Grid中反映更改刷新()?

2 个答案:

答案 0 :(得分:0)

尝试使用ObservableCollection而不是BindingList Observable实现了INotifyPropertyChange,当事情发生变化时会通知DataGridView

答案 1 :(得分:0)

通常很难回答这样的问题,因为我们通常无法访问.NET设计师的推理。

所以我试着通过猜测来理解它;我希望这不仅可以帮助你理解,还可以接受并充分利用它。

可能决定持续自动刷新的权力对于例如效果甚至用户体验。因此,他们会让您决定何时所有更新都会触发Refresh ..

Click和来自代码的调用之间存在很大差异,更不用说来自其他任务了。 UI上发生Click,因此UI应该是最新的。代码中对数据源的更改可能连续多次发生,任何频率和闪烁 UI都不会很好..

让我们尝试改变观点:不要将手头的问题视为<强>繁琐的额外任务,而是将其视为<强>控制机会刷新发生的时间。

或者更进一步改变观点:当他可能真的更喜欢刷新按钮时,您可以阻止用户 泛滥更新,也许是不显眼的计数突出的变化或新记录..

相关问题