【问题标题】:Datagridview only showing data in rows with async data access?Datagridview 仅显示具有异步数据访问权限的行中的数据?
【发布时间】:2017-10-09 06:45:20
【问题描述】:

我现在有点迷茫

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

_context 变量来自实体框架

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

但是一旦我删除了方法中第一行的注释(它只是为了测试,没用过),一切正常

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

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

它实际上只是使其工作的方法中的第一行,并且在任何地方都没有对 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;
            }
        }

    }

【问题讨论】:

    标签: c# winforms asynchronous datagridview


    【解决方案1】:

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

    【讨论】:

    • 如果你有一个标记为异步但没有等待的方法,你是否从方法中删除异步并不重要(当然我在同步尝试时删除了它),方法将同步运行,所以这不是我所说的错误,如果我删除所有异步它不起作用,如果我只是在 foreach 循环上方的方法中放置一些异步语句它工作正常,那是我的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-28
    • 2017-10-07
    • 2016-12-14
    • 2023-04-09
    • 1970-01-01
    相关资源
    最近更新 更多