首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用异步数据访问的Datagridview只在行中显示数据吗?

使用异步数据访问的Datagridview只在行中显示数据吗?
EN

Stack Overflow用户
提问于 2017-10-09 14:45:21
回答 1查看 219关注 0票数 0

我现在有点困惑

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

_context变量来自实体框架

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

但是,一旦我删除了方法第一行的注释(它只用于测试,并未使用),一切都正常

因此,在我看来,显示数据似乎需要一些异步访问

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

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

如果我把第一行放在方法的末尾,它就不会再工作了,所以只要在for-each循环调试之前有一个异步访问在工作,它就会工作

不,我不能让所有的东西都是异步的,因为我有一些错误,而且所有的东西都可以使用同步数据访问

我也没有写代码,我知道在datagridview中加载数据是一种糟糕的方式

有人知道这里出了什么问题吗?

诚挚的问候

EDIT:刚发现那一行被注释掉了,每次foreach-lopp的新迭代开始时,前一行的单元值都会被重置,不知道这是怎么发生的

代码语言:javascript
复制
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;
            }
        }

    }
EN

回答 1

Stack Overflow用户

发布于 2017-10-09 16:55:19

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46640268

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档