gpt4 book ai didi

c# - 显示表单后,DataGridViewComboBoxCell.DataSource 被设置为 null

转载 作者:太空狗 更新时间:2023-10-30 00:16:47 24 4
gpt4 key购买 nike

我在表单的 Load 事件处理程序期间将 DataGridViewComboBoxColumn 添加到 DataGridView 并设置每个 DataGridViewComboBoxCell< 的数据源 在列中。但是,一旦显示表单,每个 DataGridViewComboBoxCell 的 DataSource 都已设置为 null。这是我用来填充列及其单元格的代码:

DataGridViewComboBoxColumn comboCol;

comboCol = new DataGridViewComboBoxColumn();
comboCol.Name = "ComboCol";
comboCol.HeaderText = "Combo Column";
comboCol.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

this.dgv.Columns.Add(comboCol);

for (int i = 0; i < dgv.Rows.Count; i++)
{
// This datatable is actually populated here!
DataTable myData = PopulatedDataTable(dgv.Rows[i].Cells["info"].Value);

DataGridViewComboBoxCell DCC = new DataGridViewComboBoxCell();

DCC = (DataGridViewComboBoxCell)dgv.Rows[i].Cells["CombolCol"];
DCC.DataSource = myData;

DCC.DisplayMember = "Association"; // Association is a column in myData
DCC.ValueMember = "Association";
}

dgv.Columns["association"].Visible = false;

如果我将它放在我在表单加载后单击的按钮中,那么这段代码的作用完全符合预期,但在表单加载期间执行时,数据源被清除。有什么建议吗?

最佳答案

我最终通过处理数据网格的 DataBindingComplete 事件解决了这个问题。

显然,当您在表单的 Load 事件期间填充数据网格时,数据网格中的数据会反弹。这搞砸了我试图添加的未绑定(bind)列中的数据。

通过将我上面的代码放在 DataBindingComplete 事件处理程序中(并在开始时禁用事件处理程序并在事件结束时重新启用它),列会在适当的时间添加,并且他们的数据没有被 .NET 的愚蠢所扰乱。

关于c# - 显示表单后,DataGridViewComboBoxCell.DataSource 被设置为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5300961/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com