首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DataGridViewComboBoxCell值无效#2

DataGridViewComboBoxCell值无效#2
EN

Stack Overflow用户
提问于 2015-06-18 00:02:53
回答 1查看 249关注 0票数 2

我知道这可能是重复的,但我还没有找到一个有效的解决方案。我有一个具有组合框列的datagridview。当我不尝试设置组合框列的值时,一切都很好(即,组合框列正在填充)。当我尝试设置该值时,我得到了臭名昭著的"DataGridViewComboBoxCell值无效“错误。下面是我的代码:

代码语言:javascript
复制
//Retrieve Data
System.Data.DataRowCollection DR1 = GetData(constants.SQL_1);
System.Data.DataRowCollection DR2 = GetData(constants.SQL_2);

//Populate list for combo box column
List<string> list2 = new List<string>();
foreach (System.Data.DataRow DR in DR2)
    list2 .Add(DR["fieldname"].ToString().Trim());

//Set datasource of combo box column
DataGridViewComboBoxColumn cmb =   (DataGridViewComboBoxColumn)dgv.Columns["comboboxcolumnname"];
cmb.ValueType = typeof(string);
cmb.DataSource = list2 ;

//Populate Data Grid View
foreach (System.Data.DataRow DR in DR1)
{

    DataGridViewComboBoxCell cell = new DataGridViewComboBoxCell();
    cell.Value = DR["fieldname"].ToString();

    DataGridViewRow row = new DataGridViewRow();

    row.Cells.Add(cell);
    dgv.Rows.Add(row);

 }

如何设置组合框的值??

EN

回答 1

Stack Overflow用户

发布于 2015-06-18 02:57:57

在这个Post中,它建议根本不设置combobox列的数据源属性。下面是我所做的:

代码语言:javascript
复制
System.Data.DataRowCollection DRC1 = dictionaries.GetData(constants.SQL_1);
System.Data.DataRowCollection DRC2 = dictionaries.GetData(constants.SQL_2);

foreach (System.Data.DataRow DR1 in DRC1)
{
     DataGridViewComboBoxCell cmb = (DataGridViewComboBoxCell)dgv[0, dgv.Rows.Count - 1];

     foreach (System.Data.DataRow DR2 in DRC2)
     {
         cmb.Items.Add(DRC2["fieldname2"].ToString().Trim());
     }

     cmb.Value = DRC1["fieldname1"].ToString();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30896745

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档