【问题标题】:DataGridViewComboBoxCell value is not validDataGridViewComboBoxCell 值无效
【发布时间】:2014-07-21 10:29:15
【问题描述】:

有人可以向我解释如何将 DataGridViewComboBoxCell 添加到 dataGridView 吗? 代码是这样的:

 foreach(....){

 DataGridViewComboBoxCell cmb = new DataGridViewComboBoxCell();
//cmb.item.add(....

dataGridView.Rows.Add(new object[] {cmb,name,surname});   } 

网格中的第一个单元格是 DataGridViewComboBoxColumn 类型,我尝试将 cmb 更改为 DataGridViewComboBoxColumn,但仍然没有。

我处理了 DataError,所以我没有收到“值无效”错误,但我在 datagridview 中的组合框是空的。

【问题讨论】:

    标签: c# datagridview datagridviewcombobox datagridviewcomboboxcell


    【解决方案1】:

    好的,我解决了这个问题。看来您必须一步一步地向单元格添加值。

    我将给出一般性的解释,因为似乎很多人对此有疑问。假设您有 3 列的 DataGridView,DataGridViewTextBoxCell、DataGridViewComboBoxColumn、DataGridViewCheckBoxCell 按顺序排列。现在,您必须使用 Desinger 制作这三列,否则它将无法正常工作。

    所以现在你想向网格添加特定的值,每一行代表一个人。 在设计器中看起来像

              Name  PhoneNumberS   Married
      ..*.. |.....|..............|.........|.... 
    

    所以你想将他的名字添加到 textboxcell,将电话号码列表添加到 comboboxcell 并检查 checkboxcell 如果他结婚了。并为您列表中的每个人重复。

    这是伪代码:

     foreach(Person p in people.getAll()){
    
    /////MAKE NEW CELL FOR EACH VALUE
     DataGridViewTextBoxCell name= new DataGridViewTextBoxCell();
     name.Value = p.name;
    
     DataGridViewTextBoxCell phones= new DataGridViewTextBoxCell();
    
     foreach(Int Pnumber in p.numbers){
        phones.items.add(Pnumber);
      }
    
      DataGridViewCheckBoxCell ismarried = new DataGridViewCheckBoxCell();
             ismarried.Value = p.married;
    
     ///////// MAKE NEW ROW AND ADD CELLS
     DataGridViewRow row = new DataGridViewRow();
      row .Cells.Add(name);
      row .Cells.Add(phones);
      row .Cells.Add(ismarried );
    
       ///// ADD ENTIRE ROW TO DATA GRID
       dataGridView.Rows.Add(row);
       }
    

    重复一遍,您首先必须使用 DESIGNER 将列添加到网格中,并且当您在代码中将单元格添加到行时,它必须与 DESIGNER 中看到的顺序完全相同。

    【讨论】:

      【解决方案2】:

      我认为你想添加一个 DataGridViewComboBoxColumn 类型的列。

      DataGridViewComboBoxColumn d = new DataGridViewComboBoxColumn();
              d.Name = "Drinks";
              d.Items.Add("Coca-Cola");
              d.Items.Add("Sprite");
      
              dataGridView1.Columns.Add(d);
      

      我希望它会有所帮助。

      【讨论】:

      • 是的,我尝试使用 DataGridViewComboBoxColumn 仍然一无所获。
      • 不,网格中的组合框仍然是空的。我正在尝试使用 DataGridViewComboBoxColumn cmb = ((DataGridViewComboBoxColumn)l.dataGridView1.Columns["Column1"]);但它将所有组合框项目设置为相同的项目
      • 阅读这篇文章。 telerik.com/forums/… 是telerik 组件,不过差不多。希望对您有所帮助。
      【解决方案3】:

      关于如何解决这个问题有不同的方法。我发布这个答案是因为我正在处理类似的情况并找到了一个不错的解决方案。 DataGridView 必须放在您的公式上,其余部分(在本例中)在代码中完成。 首先,我们从 DataGridView 的 DataSource 开始。

          DataTable myData = new DataTable();
          myDataGridView.AutoGenerateColumns = false;
      

      关闭自动生成很重要,因此我们可以根据需要设置列。现在向我们的 DataTable 添加一些数据。在这个例子中猫:

          myData.Columns.Add("Name", typeof(string));
          myData.Columns.Add("Gender", typeof(string));
      
          myData.Rows.Add(new object[] {"Ser Pounce", "male"});
          myData.Rows.Add(new object[] {"Fluffy", "male"});
          myData.Rows.Add(new object[] {"Peach", "female"});
      

      在我们指定数据及其列之后,我们可以在 DataGridView 中创建特定的列。

           DataGridViewTextBoxColumn nameColumn = new DataGridViewTextBoxColumn();
           nameColumn.HeaderText = "Cat name";
           nameColumn.DataPropertyName = "Name"; 
           nameColumn.ReadOnly = true;
      
           DataGridViewComboBoxColumn genderColumn = new DataGridViewComboBoxColumn ();
           List<string> genderList = new List<string>() { "male", "female", "unknown" };
           genderColumn.DataSource = genderList;
           genderColumn.HeaderText = "Gender";
           genderColumn.DataPropertyName = "Gender";
           genderColumn.ValueType = typeof(string);
      
           myDataGridView.DataSource = myData;
           myDataGridView.Columns.AddRange(nameColumn, genderColumn);
      

      在添加定义的列之前,将我们的数据添加为 DataGridView 的数据源。请注意,每列 (DataGridView) 的 DataPropertyName 必须与我们在 DataTable 中为列指定的名称相同。 HeaderText 可以以不同的方式命名。最后匹配列和数据的ValueType。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-13
        • 1970-01-01
        • 1970-01-01
        • 2016-10-17
        • 1970-01-01
        • 2014-01-02
        相关资源
        最近更新 更多