我正在使用async和await函数开发一个Windows窗体应用程序。我有多个依赖的组合框控件,其中c2取决于c1值,datagridview基于输入加载。当用户点击datagridview单元格时,c1文本根据选定的单元格值设置,但即使选择的单元格值返回字符串,c2文本也设置为null。请在下面找到代码片段
private void dgvAsset_SelectionChanged(object sender, EventArgs e)
{
DataGridViewCell cell = null; // set cell initial value to null
foreach (DataGridViewCell selectedcell in dgvAsset.SelectedCells)
{
cell = selectedcell; // load selected cell value
break;
}
if (cell != null)
{
DataGridViewRow sr = cell.OwningRow;
assetID = Convert.ToInt32(sr.Cells["AssetID"].Value.ToString());
txtAssId.Text = sr.Cells["Asset_ID"].Value.ToString();
cbAssType.Text = sr.Cells["Asset_Type"].Value.ToString();
txtAsSerNo.Text = sr.Cells["AssetSerialNo"].Value.ToString();
txtAssDesc.Text = sr.Cells["Asset_Description"].Value.ToString();
dtAFrom.Text = sr.Cells["AllocatedFrom"].Value.ToString();
dtATo.Text = sr.Cells["AllocatedTo"].Value.ToString();
cbProjCode.Text = sr.Cells["Project_Code"].Value.ToString();
// this.cbProjCode.SelectionChangeCommitted -= new EventHandler(cbProjCode_SelectionChangeCommitted);
// string port = sr.Cells["Portfolio"].Value.ToString();
/* the below 3 combobox are set to null*/
cbPortfolio.Text = Convert.ToString(sr.Cells["Portfolio"].FormattedValue.ToString())
cbPOwner.Text = sr.Cells["Primary_Owner"].Value.ToString();
cbSOwner.Text = sr.Cells["Seconday_Owner"].Value.ToString();
}
}
public async Task LoadProjectCode()
{
List<string> projectCode = new List<string>();
projectCode = await func.LoadProjectCode();
// this.cbProjCode.SelectedIndexChanged -= new EventHandler(cbProjCode_SelectedIndexChanged);
cbProjCode.DataSource = projectCode;
cbProjCode.SelectedIndex = -1;
}
private async void cbProjCode_SelectionChangeCommitted(object sender, EventArgs e)
{
cbProjCode.Enabled = false;
cbAssType.Enabled = true;
cbPortfolio.Enabled = true;
cbPOwner.Enabled = true;
cbSOwner.Enabled = true;
txtAssDesc.Enabled = true;
txtAsSerNo.Enabled = true;
txtAssId.Enabled = true; ProjectCodeId = await func.GetProjectCodeID(cbProjCode.Text);
await LoadPortfolio();
}
当我没有使用异步并且等待加载组合框并且使用了combobox selectedindexchange事件而不是之前提交的selection。时,相同的代码工作正常。