Datagridview仅在具有异步数据访问的行中显示数据?
问题描述:
我现在有些困惑Datagridview仅在具有异步数据访问的行中显示数据?
我,如果我调试下面的方法,设置在DataGrid
的_context变种是从实体框架
每行的单元格的所有数据的方法这样,所有的数据被设置为细胞,但数据未显示
,但只要我删除第一行注释的方法(它只是用于测试,无处使用的)一切工作
所以对我来说,它好像有显示
我搜索的谷歌,但没有发现任何类似的数据需要进行一些异步访问
它真的只是在这使得该方法的第一行工作,并没有任何地方的任何参考公正的任务列表
如果我把第一行的方法结束它不再工作,所以它的工作,只要有一个异步访问工作之前,每个循环都被调试
并且不,我不能使所有的异步导致我h广告的一些错误,以及一切工作与同步数据访问
没有写代码,以及,我知道这是在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;
}
}
}
答
将方法标记为async时,它没有等待操作符是没有意义的。注释行中的dummy等待运算符只是在同步执行后的所有其他代码。 因此,使其工作的最简单方法是删除无用的异步修改器。
如果从方法中删除异步或不同意(当然,当我同步尝试时删除它) 如果您有一个标记为async但没有等待的方法,该方法将运行同步,所以这不是错误 正如我所说,如果我删除everythign异步它不起作用,如果我只是在foreach循环上面的方法中放置一些异步语句它工作正常,这是我的问题 – Claus