0

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

 foreach(....){

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

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

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

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

4

3 回答 3

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 中看到的顺序完全相同。

于 2014-06-01T14:53:55.870 回答
0

我认为您要添加 DataGridViewComboBoxColumn 类型的列。

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

        dataGridView1.Columns.Add(d);

我希望它会有所帮助。

于 2014-06-01T07:50:55.500 回答
0

关于如何解决这个问题有不同的方法。我发布这个答案是因为我正在处理类似的情况并找到了一个不错的解决方案。必须将 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。

于 2016-08-02T13:56:19.213 回答