【问题标题】:set value in combobox inside datagridview在 datagridview 内的组合框中设置值
【发布时间】:2015-10-05 01:01:28
【问题描述】:

我有内部带有组合框的 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";
  }

通过代码设置我的意思是:以编程方式(取决于数据表中的值)。 我根本不会出错。该值根本不会显示在组合框中

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

?dgShuffle.Rows[i].Cells[6]

{DataGridViewComboBoxCell { ColumnIndex=6, RowIndex=0 }} [System.Windows.Forms.DataGridViewComboBoxCell]: {DataGridViewComboBoxCell { ColumnIndex=6, RowIndex=0 }} 基数:{DataGridViewComboBoxCell { ColumnIndex=6, RowIndex=0 }} AccessibilityObject:{System.Windows.Forms.DataGridViewCell.DataGridViewCellAccessibleObject} 列索引:6 内容边界:{X = 0 Y = 12 宽度 = 0 高度 = 0} 上下文菜单条:空 默认新行值:空 显示:假 编辑格式化值:“” EditType: {Name = "DataGridViewComboBoxEditingControl" FullName = "System.Windows.Forms.DataGridViewComboBoxEditingControl"} ErrorIconBounds: {X = 0 Y = 0 宽度 = 0 高度 = 0} 错误文本:“” 格式化值:“” FormattedValueType: {Name = "String" FullName = "System.String"} 冻结:假 有样式:假 InheritedState:可调整大小 |可调整大小集 |可见的 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:假 OwningColumn: {DataGridViewComboBoxColumn { Name=cmb, Index=6 }} OwningRow: {DataGridViewRow { 索引=0 }} PreferredSize: {宽度 = 43 高度 = 26} 只读:假 可调整大小:真 行索引:0 选择:假 尺寸:{宽度 = 100 高度 = 24} 样式:{DataGridViewCellStyle { }} 标签:空 工具提示文本:“” 值:空 值类型:{Name = "Object" FullName = "System.Object"} 可见:真

下一次尝试:

  1. 我创建了一个新的 winforms 项目
  2. 将 datagridview 拖到默认表单(从工具箱中)
  3. 将此代码复制到 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";
        }
    

我错过了什么???

【问题讨论】:

  • “我无法在代码中设置索引”是什么意思?你的目标到底是什么?
  • try/catch 包裹它,你会收到错误吗?我不明白需要强制转换,代码 dgShuffle.Rows[i].Cells[6].Value = "real"; 应该可以正常工作,只要该值在下拉列表中可用。
  • 不太明白你的问题。我刚刚测试了你的代码块,它工作正常。您是否设置了错误的列索引?
  • 我编辑了我的问题。
  • 您的 ComboBoxColumn 显示,您可以在运行时更改其值但不能以编程方式? Ps:仍然没有在您的代码中看到任何错误。您能否提供影响您的 DataGridView 的其他代码?也许尝试删除您的 DataGridView 并创建一个新的以避免设计时错误?

标签: c# winforms datagridview combobox


【解决方案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;

【讨论】:

  • 但是如果我想根据我输入到组合框的值来过滤组合框呢?
  • 是否可以为同一个列表中的不同行设置不同的组合框值?
  • @Dimi,组合框列的数据源在所有行之间共享。要为不同的行获取不同的项目 - 您应该为每个组合框单元格设置自己的项目集合。检查这个:https://stackoverflow.com/a/27202221/1565525
猜你喜欢
  • 2011-10-11
  • 2021-02-20
  • 1970-01-01
  • 2012-08-10
  • 2014-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多