Datagridview只显示具有异步数据访问权限的行中的数据?

时间:2017-10-09 06:45:21

标签: c# winforms asynchronous datagridview

我现在有点困惑

我有一个方法可以在Datagrid中设置每行单元格的所有数据

_context var来自实体框架

如果我像这样调试下面的方法,所有数据都设置为单元格,但不显示数据

但是一旦我删除了方法第一行的注释(它仅用于测试,无处使用)一切正常

所以对我而言,似乎显示数据需要一些异步访问

我在谷歌搜索但没有找到类似的东西

它实际上只是该方法的第一行使其工作,并且没有任何地方对justATest列表的引用

如果我将第一行放到方法的末尾它再也不起作用,那么只要在调试for-each循环之前有异步访问就可以正常工作

并且我不能让所有东西都异步,因为我有一些错误,一切都适用于同步数据访问

也没有编写代码,我知道在datagridview中加载数据是一种坏方法

有谁知道这里有什么不对吗?

最好的问候

编辑:刚发现该行被注释掉了,每次foreach-lopp的新迭代开始时,前一行的单元格值都会重置,请不要发生这种情况

private async Task InitializeProcessGroupsDgv()
    {
        //List<Test> justATest = await _context.Test.ToListAsync();

        List<ProcessGroup> processGroups =  _context.ProcessGroup.ToList();

        _view.Dgv_ProcessGroups.AutoGenerateColumns = false;
        _view.Dgv_ProcessGroups.DataSource = processGroups;

        // that the cheboxes for the locks can have 3 states
        ((DataGridViewCheckBoxColumn)_view.Dgv_ProcessGroups.Columns[13]).ThreeState = true;
        ((DataGridViewCheckBoxColumn)_view.Dgv_ProcessGroups.Columns[14]).ThreeState = true;

        foreach (DataGridViewRow row in _view.Dgv_ProcessGroups.Rows)
        {
            // set all TestOption-Checkboxes
            for (int i = 0; i < 8; i++)
            {
                if (_curTestRes != null)
                {
                    row.Cells[i + 1].Value = ((ProcessGroup)row.DataBoundItem).TestResourceProcessGroup.Where(x => x.TestResourceID == _curTestRes.ID).First().TestOptionTestRessourceProcessGroup.Where(x => x.TestOption.Name == row.Cells[i+1].OwningColumn.HeaderText).FirstOrDefault()?.Activated;
                }
                else
                {
                    row.Cells[i + 1].Value = false;
                }
            }

            var testResProcGr = ((ProcessGroup)row.DataBoundItem).TestResourceProcessGroup?
                .Where(trpg => trpg.TestResourceID == _curTestRes?.ID)?.SingleOrDefault();

            // load NrOfImpressions
            row.Cells[10].Value = testResProcGr?.NrOfImpressions;

            // set the Lock-CheckBoxes
            DataGridViewCheckBoxCell dinCell = (DataGridViewCheckBoxCell)row.Cells[13];
            DataGridViewCheckBoxCell astmCell = (DataGridViewCheckBoxCell)row.Cells[14];

            List<IntrusionBody> ibs = _context.IntrusionBody.Where(x => x.ProcessGroupID == ((ProcessGroup)row.DataBoundItem).ID).ToList();

            foreach (var item in ibs)
            {
                ((DataGridViewComboBoxCell)row.Cells[12]).Items.Add(item);
            }

            ((DataGridViewComboBoxCell)row.Cells[12]).ValueMember = "ID";
            ((DataGridViewComboBoxCell)row.Cells[12]).DisplayMember = "Name";

            if (testResProcGr == null)
            {
                // if new TestRessource => disable lock checkboxes
                dinCell.ReadOnly = true;
                dinCell.Value = CheckState.Indeterminate;
                dinCell.FlatStyle = FlatStyle.Flat;
                dinCell.Style.ForeColor = Color.DarkGray;

                astmCell.ReadOnly = true;
                astmCell.Value = CheckState.Indeterminate;
                astmCell.FlatStyle = FlatStyle.Flat;
                astmCell.Style.ForeColor = Color.DarkGray;
            }
            else
            {
                // DIN
                if (testResProcGr.DINLocked == null)
                    dinCell.Value = CheckState.Indeterminate;
                else
                    dinCell.Value = (bool)testResProcGr.DINLocked ? CheckState.Checked : CheckState.Unchecked;

                // ASTM
                if (testResProcGr.ASTMLocked == null)
                    astmCell.Value = CheckState.Indeterminate;
                else
                    astmCell.Value = (bool)testResProcGr.ASTMLocked ? CheckState.Checked : CheckState.Unchecked;

                row.Cells[11].Value = testResProcGr.NrOfTestPerDay;
                ((DataGridViewComboBoxCell)row.Cells[12]).Value = testResProcGr.IntrusionBody;
                row.Cells[15].Value = testResProcGr.Active;
                row.Cells[16].Value = testResProcGr.TestResourceProcessGroupCertification.Where(x => x.Date.Year == DateTime.Now.Year).FirstOrDefault()?.DIN;
                row.Cells[17].Value = testResProcGr.TestResourceProcessGroupCertification.Where(x => x.Date.Year == DateTime.Now.Year).FirstOrDefault()?.ASTM;
            }
        }

    }

1 个答案:

答案 0 :(得分:0)

当一个方法没有await运算符时将其标记为异步没有意义。注释行中的虚拟await运算符只会使其后的所有其他代码同步执行。 因此,使其工作的最简单方法是删除无用的异步修改器。