Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在每个单元格中设置具有不同数据源的DataGridView ComboxColumn?_C#_.net_Datagridview - Fatal编程技术网

C# 如何在每个单元格中设置具有不同数据源的DataGridView ComboxColumn?

C# 如何在每个单元格中设置具有不同数据源的DataGridView ComboxColumn?,c#,.net,datagridview,C#,.net,Datagridview,我正在设置一个DataGridViewComboBoxColumn,如下所示: var newColumn = new DataGridViewComboBoxColumn() { Name = "abc" }; newColumn.DataSource = new string[] { "a", "b", "c" }; dgv.Columns.Add(newColumn); foreach (DataGridViewRow row in dgv.Rows) { var ce

我正在设置一个
DataGridViewComboBoxColumn
,如下所示:

var newColumn = new DataGridViewComboBoxColumn() {
    Name = "abc"
};
newColumn.DataSource = new string[] { "a", "b", "c" }; 
dgv.Columns.Add(newColumn);
foreach (DataGridViewRow row in dgv.Rows) {
    var cell = (DataGridViewComboBoxCell)(row.Cells["abc"]);        
    cell.DataSource = new string[] { "a", "c" };                        
}
这是可行的:每一行在该列中都有一个下拉框,用a、b、c填充

但是,现在我想为某些行修剪列表。我试图按如下方式设置每行的列表:

var newColumn = new DataGridViewComboBoxColumn() {
    Name = "abc"
};
newColumn.DataSource = new string[] { "a", "b", "c" }; 
dgv.Columns.Add(newColumn);
foreach (DataGridViewRow row in dgv.Rows) {
    var cell = (DataGridViewComboBoxCell)(row.Cells["abc"]);        
    cell.DataSource = new string[] { "a", "c" };                        
}
但是,此代码无效-每行仍显示“a”、“b”、“c”

我尝试用
新列表
新绑定列表
替换
新字符串[]
,但均无效

我还尝试删除设置
newColumn.DataSource
的代码,但是列表是空的


我应该如何正确地进行这项工作?

以下几点对我很有用:

DataGridViewComboBoxColumn newColumn = new DataGridViewComboBoxColumn();
newColumn.Name = "abc";
newColumn.DataSource = new string[] { "a", "b", "c" };
dataGridView1.Columns.Add(newColumn);

foreach (DataGridViewRow row in dataGridView1.Rows)
{
    DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)(row.Cells["abc"]);
    cell.DataSource = new string[] { "a", "c" };
}
for (int row = 0; row < dataGridView1.Rows.Count; row++)
{
   DataGridViewComboBoxCell cell = 
       (DataGridViewComboBoxCell)(dataGridView1.Rows[row].Cells["abc"]);
   cell.DataSource = new string[] { "f", "g" };
}
您也可以尝试(这对我也适用):

for(int row=0;row
另一个选项是尝试在行级别进行数据绑定。尝试使用事件OnRowDataBound事件。然后,您可以根据该行的内容以编程方式设置组合框中的内容


当然,这假定您正在对网格进行数据绑定。

我正在进行数据绑定,但这是winforms,而不是web。winforms datagridview似乎没有此事件..嗯,这也适用于我-在干净的测试项目中。这一定是我在做不同的事情……好吧,问题在于我的DataGridView将AutoSizeColumnMode设置为AllCells。我认为这是在设置数据源(或其他)之前验证单元格的值。