【发布时间】: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