使用异步填充后,DataGridView隐藏滚动条

时间:2020-09-16 04:08:32

标签: c# asynchronous datagridview task

在我的表单中,我使用制表符控件分隔了4个数据网格。

当我尝试用异步任务填充它时,滚动条消失了。

这是我的代码

        await Task.Run(() =>
        {
            PopulateHistoryAll(items);
            PopulateHistoryParkerIn(parkerIn);
            PopulateHistoryParkerOut(parkerOut);
            PopulateHistoryMonthlyIn(monthlyIn);
            PopulateHistoryMonthlyOut(monthlyOut);
        });

我尝试使用此代码

        this.Invoke((MethodInvoker)delegate
        {
            //DataGridview Refreshment
            dgParkerIn.Enabled = true;
            dgParkerIn.ScrollBars = ScrollBars.Both;
        });

但无法正常工作。

请帮助我,谢谢:)

2 个答案:

答案 0 :(得分:0)

这是我的用于填充表的代码。他们都是一样的

    private void PopulateHistoryAll(IEnumerable<HistoryModel> items)
    {
        dgHistoryAll.Rows.Clear();
        if (items.Count() > 0)
            dgHistoryAll.Rows.Add(items.Count());

        var row = 0;
        foreach (var item in items)
        {
            dgHistoryAll[dtlAllRow.Index, row].Value = row + 1;
            dgHistoryAll[dtlAllTransitId.Index, row].Value = item.Id;
            dgHistoryAll[dtlAllTerminal.Index, row].Value = item.Terminal;
            dgHistoryAll[dtlAllORNo.Index, row].Value = item.ORNumber;
            dgHistoryAll[dtlAllPlateNo.Index, row].Value = item.PlateNo;
            dgHistoryAll[dtlAllTicketNo.Index, row].Value = item.TicketNumber;
            dgHistoryAll[dtlAllDateTimeIn.Index, row].Value = item.TimeIn;
            dgHistoryAll[dtlAllDateTimeOut.Index, row].Value = item.TimeOut;
            dgHistoryAll[dtlAllDurationOfStay.Index, row].Value = item.Duration;
            dgHistoryAll[dtlAllRatesName.Index, row].Value = item.RateName;
            dgHistoryAll[dtlAllCoupon.Index, row].Value = item.Coupon;
            dgHistoryAll[dtlAllMonthlyRFID.Index, row].Value = item.MonthlyRFID;
            dgHistoryAll[dtlAllMonthlyName.Index, row].Value = item.MonthlyName;
            row++;
        }
        this.Invoke((MethodInvoker)delegate
        {
            dgHistoryAll.ScrollBars = ScrollBars.Both; 
        });
    }

答案 1 :(得分:0)

我终于找到了解决方法。

虽然这很奇怪...

我试图删除数据网格,并添加创建另一个,然后....

它有效,idk为什么。

相关问题