gpt4 book ai didi

c# - DataGridViewComboBox 数据源

转载 作者:行者123 更新时间:2023-11-30 18:01:07 28 4
gpt4 key购买 nike

我目前有一个 2 列宽的 DataGridView,第一列是 DataGridViewTextBoxColumn,第二列是 DataGridViewComboBoxColumn。我还有一个预生成的通用列表(字符串),用作每一行的 DataGridViewComboBox 的数据源。

最后,我有一个循环遍历一系列字符串并相应地解析它们,使用如下所示将提取的值应用于相应的单元格:

dataGridView.Rows.Add("Column1Text", "Column2Text");

gridview 数据按预期填充,DataGridViewComboBox 正确显示理想项目。

问题是,单击 DataGridViewComboBox 时,不会下拉任何项目。我已经检查过 DataGridViewComboBox 是否包含项目。如果有任何相关性,DataGridViewTextBoxColumn 的 AutoSizeMode 将设置为“Fill”。

关于我可能做错了什么的任何见解?单击给定单元格时是否必须手动下拉项目?谢谢。

更新

在将通用列表绑定(bind)为数据源方面,我尝试了两种不同的方法。

第一个是通过以下方式绑定(bind)整个列本身的数据源:

col_key.DataSource = KeyList;

第二种方法是在相应行中绑定(bind)每个新 DataGridViewComboBoxCell 的数据源:

(DataGridViewComboBoxCell)(row.Cells[1]).DataSource = KeyList;

这两种方法都会编译并在运行时正确添加必要的项目,但点击时不会下拉任何项目。

最佳答案

我选择在 CellEnter 事件中处理这个:

private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 2)
{
DataGridViewComboBoxCell cell = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewComboBoxCell;
if (cell.DataSource == null)
{
cell.DataSource = this._ComboItemsBindingSource;
cell.DisplayMember = "Value"; //lite-weight wrapper on string
cell.ValueMember = "Value"; //where Value is a property
}
}
}

关于c# - DataGridViewComboBox 数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9455419/

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