首页 文章

在datagridview里面的组合框中设置值

提问于
浏览
0

我在内部使用了组合框的Datagridview,我无法在代码中设置索引我读取thisthis - 两者都不起作用 .

这是我的代码:

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";
  }

按代码设置我的意思是:以编程方式(取决于datatble中的值) . 我根本不会得到错误 . 该值根本不会显示在组合框中

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

?dgShuffle.Rows [i] .Cells [6] {DataGridViewComboBoxCell {ColumnIndex = 6,RowIndex = 0}} [System.Windows.Forms.DataGridViewComboBoxCell]:{DataGridViewComboBoxCell {ColumnIndex = 6,RowIndex = 0}} base:{ DataGridViewComboBoxCell {ColumnIndex = 6,RowIndex = 0}} AccessibilityObject: ColumnIndex:6 ContentBounds: ContextMenuStrip:null DefaultNewRowValue:null显示:false EditedFormattedValue:“”EditType:{Name =“DataGridViewComboBoxEditingControl”FullName =“System.Windows.Forms.DataGridViewComboBoxEditingControl”} ErrorIconBounds: ErrorText:“”FormattedValue:“” FormattedValueType:{Name =“String”FullName =“System.String”} Frozen:false HasStyle:false InheritedState:Resizable | ResizableSet | Visible InheritedStyle:{DataGridViewCellStyle {BackColor = Color [Window],ForeColor = Color [ControlText],SelectionBackColor = Color [Highlight],SelectionForeColor = Color [HighlightText],Font = [Font:Name = Microsoft Sans Serif,Size = 7.8,Units = 3,GdiCharSet = 177,GdiVerticalFont = False],WrapMode = False,Alignment = MiddleLeft}} IsInEditMode:false OwningColumn:{DataGridViewComboBoxColumn {Name = cmb,Index = 6}} OwningRow:{DataGridViewRow } PreferredSize: ReadOnly:false Resizable:true RowIndex:0 Selected:false Size: Style:{DataGridViewCellStyle {}} Tag:null ToolTipText:“”Value:null ValueType:{ Name =“Object”FullName =“System.Object”}可见:true

下一步尝试:

  • 我创建了一个新的winforms项目

  • 将datagridview拖动到默认表单(从工具箱中)

  • 将此代码复制到Form1_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";
    }

我错过了什么?

1 回答

  • 1

    您正在使用与数据源绑定的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;
    

相关问题