C# 在datagridview内的组合框中设置值

C# 在datagridview内的组合框中设置值,c#,winforms,datagridview,combobox,C#,Winforms,Datagridview,Combobox,我有内置combobox的Datagridview,无法在代码中设置索引 我读书,但都不工作 这是我的密码: dgShuffle.DataSource = dtCards; DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn(); cmb.HeaderText = "Select Data"; cmb.Name = "cmb"; cmb.MaxDropDownItems = 2; cmb.Item

我有内置combobox的Datagridview,无法在代码中设置索引 我读书,但都不工作

这是我的密码:

  dgShuffle.DataSource = dtCards;
  DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
  cmb.HeaderText = "Select Data";
  cmb.Name = "cmb";
  cmb.MaxDropDownItems = 2;
  cmb.Items.Add("real");
  cmb.Items.Add("sham");

  dgShuffle.Columns.Add(cmb);
  for (int i = 0; i < dgShuffle.RowCount; i++)
  {
       (dgShuffle.Rows[i].Cells[6] as DataGridViewComboBoxCell).Value = "real";
        // dgShuffle.Rows[i].Cells[6].Value = "real";
  }
dgShuffle.DataSource=dtCards;
DataGridViewComboBoxColumn cmb=新DataGridViewComboxColumn();
cmb.HeaderText=“选择数据”;
cmb.Name=“cmb”;
cmb.MaxDropDownItems=2;
cmb.项目。添加(“真实”);
cmb.项目。添加(“假”);
dgShuffle.Columns.Add(cmb);
对于(int i=0;i
通过代码设置,我的意思是:以编程方式(取决于datatble中的值)。 我一点也不犯错误。该值根本不显示在组合框中

我检查了组合框的索引,这是正确的,在即时窗口的输出下面:

?dgShuffle.Rows[i].单元格[6]

{DataGridViewComboxCell{ColumnIndex=6,RowIndex=0}}[System.Windows.Forms.DataGridViewComboxCell]:{DataGridViewComboxCell{ColumnIndex=6,RowIndex=0} 基:{DataGridViewComboxCell{ColumnIndex=6,RowIndex=0} AccessibilityObject:{System.Windows.Forms.DataGridViewCell.DataGridViewCellAccessibleObject} 栏目索引:6 ContentBounds:{X=0 Y=12宽度=0高度=0} ContextMenuStrip:null DefaultNewRowValue:null 显示:false EditedFormattedValue:“ 编辑类型:{Name=“DataGridViewComboxeditingControl”FullName=“System.Windows.Forms.DataGridViewComboxeditingControl”} 错误边界:{X=0 Y=0宽度=0高度=0} 错误文本:“ FormattedValue:“ FormattedValueType:{Name=“String”FullName=“System.String”} 冻结:错误 哈斯泰勒:错 InheritedState:可调整大小|可调整大小集|可见 继承样式:{DataGridViewCellStyle{BackColor=Color[Window],ForeColor=Color[ControlText],SelectionBackColor=Color[HighlightText],SelectionForeColor=Color[HighlightText],Font=[Font:Name=Microsoft Sans Serif,Size=7.8,Units=3,GdiCharSet=177,GdiVerticalFont=False],WrapMode=False,Alignment=Middleft}} IsInEditMode:false OwningColumn:{DataGridViewComboxColumn{Name=cmb,Index=6} OwningRow:{DataGridViewRow{Index=0}} PreferredSize:{宽度=43高度=26} 只读:false 可调整大小:true 行索引:0 所选:false 尺寸:{宽度=100高度=24} 样式:{DataGridViewCellStyle{} 标签:空 工具文字:“ 值:null ValueType:{Name=“Object”FullName=“System.Object”} 可见:正确

下一次尝试:
  • 我创建了一个新的winforms项目
  • 将datagridview拖动到默认窗体(从工具箱中)
  • 将此代码复制到Form1\u Load:

        DataTable dtCards;
        dtCards = new DataTable();
        dtCards.Columns.Add("printedString");
        dtCards.Rows.Add("1");
        dtCards.Rows.Add("2");
        dtCards.Rows.Add("3");
        dtCards.Rows.Add("4");
        dataGridView1.DataSource = dtCards;
    
        DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
        cmb.HeaderText = "Select Data";
        cmb.Name = "cmb";
        cmb.MaxDropDownItems = 2;
        cmb.Items.Add("real");
        cmb.Items.Add("sham");
    
        dataGridView1.Columns.Add(cmb);
        for (int i = 0; i < dataGridView1.RowCount; i++)
        {
            (dataGridView1.Rows[i].Cells[6] as DataGridViewComboBoxCell).Value = "real";
            // dgShuffle.Rows[i].Cells[6].Value = "real";
        }
    
    DataTable数据卡;
    dtCards=新数据表();
    dtCards.Columns.Add(“printedString”);
    数据卡。行。添加(“1”);
    数据卡。行。添加(“2”);
    数据卡。行。添加(“3”);
    数据卡。行。添加(“4”);
    dataGridView1.DataSource=dtCards;
    DataGridViewComboBoxColumn cmb=新DataGridViewComboxColumn();
    cmb.HeaderText=“选择数据”;
    cmb.Name=“cmb”;
    cmb.MaxDropDownItems=2;
    cmb.项目。添加(“真实”);
    cmb.项目。添加(“假”);
    dataGridView1.Columns.Add(cmb);
    对于(int i=0;i

  • 我缺少什么???

    您正在使用绑定到数据源的datagridview。您需要在数据源中指定该值

    为数据源中的
    DataGridViewComboBoxColumn
    添加值
    然后将
    DataGridViewComboBoxColumn.DataPropertyName
    设置为数据源的列/属性的名称

    DataTable dtCards;
    dtCards = new DataTable();
    dtCards.Columns.Add("printedString");
    dtCards.Columns.Add("comboboxValue", typeof(String)); //adding column for combobox
    dtCards.Rows.Add("1", "real");
    dtCards.Rows.Add("2", "real");
    dtCards.Rows.Add("3", "real");
    dtCards.Rows.Add("4", "real");
    
    
    DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
    cmb.HeaderText = "Select Data";
    cmb.Name = "cmb";
    cmb.MaxDropDownItems = 2;
    cmb.Items.Add("real");
    cmb.Items.Add("sham");
    cmb.DataPropertyName = "comboboxValue"; //Bound value to the datasource
    
    dataGridView1.Columns.Add(cmb);
    dataGridView1.DataSource = dtCards;
    

    “我不能在代码中设置索引”你是什么意思?你的目标到底是什么?用
    try/catch
    把它包装起来,你会发现错误吗?我不理解强制转换的需要,代码
    dgShuffle.Rows[I].Cells[6].Value=“real”就可以正常工作。我不太理解您的问题。我刚刚测试了您的代码块,它运行良好。是否设置了错误的列索引?我编辑了我的问题。您的ComboBoxColumn显示可以在运行时更改其值,但不能以编程方式更改?Ps:仍然没有看到代码中有任何错误。您能否提供其他影响DataGridView的代码?可能尝试删除DataGridView并创建新的DataGridView以避免设计时错误?但如果我想根据输入到combobox的值筛选combobx,该怎么办?是否可以为同一列表中的不同行设置不同的combobox值?@Dimi,所有行之间共享的combobox列的数据源。为了获得不同行的不同项,您应该为每个组合框单元格设置自己的项集合。选中此项: