插入列:   DataGridViewComboBoxColumn c = new DataGridViewComboBoxColumn();
            c.Name = "deName";
            c.HeaderText = "数据元名称";
            c.Width = 100;
            dataGridView.Columns.Insert(2, c);
if (((DataGridViewComboBoxCell)dataGridView.Rows[e.RowIndex].Cells["deName"]).Items.Count > 0)
                {
                   
                    ((DataGridViewComboBoxCell)dataGridView.Rows[e.RowIndex].Cells["deName"]).Items.Clear();
                }然后在删除其中的项的时候不起作用啊。为什么?如何删除?remove方法也不行啊。

解决方案 »

  1.   

    ((DataGridViewComboBoxCell)dataGridView.Rows[e.RowIndex].Cells["deName"]).Items.Clear();
    不是这么写的吧   你搞个强制转换算是什么意思
      

  2.   

    回1楼:datagridview的中其他列的数据是绑定的,但这列不是。
    回2楼:这样转换以后,可以添加数据,即((DataGridViewComboBoxCell)dataGridView.Rows[e.RowIndex].Cells["deName"]).Items.add("001")是可以的。
    如果不这么写,有没有别的更好的方法?
      

  3.   

    这样用:
            ((DataGridViewComboBoxColumn)dataGridView1.Columns[列索引]).Items.Clear();
      

  4.   

    4楼的大哥,这样也不行啊。刚试了。
    原来那个列的item.count还是10条10条的增加的,改成这个好,直接20 30 40条这样的增加了。
    快崩溃了。
      

  5.   

    增加的部分如下:for (int i = 0; i < list.Count; i++)//取出deid和cname 加入comobox
                    {
                        string s = list[i].ToString();
                        string id = s.Remove(11);//标准id
                        string name = s.Remove(0, 13);
                        ((DataGridViewComboBoxCell)dataGridView.Rows[e.RowIndex].Cells["deName"]).Items.Add(name);
                        //hs.Add(name, id);
                    }
                    string s1 = list[0].ToString();
                    string name1 = s1.Remove(0, 13);
                    ((DataGridViewComboBoxCell)dataGridView.Rows[e.RowIndex].Cells["deName"]).Value = name1;//只是显示的效果其实并不是add的001 这个要和第一个值一样!!!我是放在一个list中,读取list中内容,然后取出我想要的部分,再加入。
      

  6.   

    remove也没效果但也不抛异常。int a = ((DataGridViewComboBoxCell)dataGridView.Rows[e.RowIndex].Cells["deName"]).Items.Count;                  
                    if (a > 0)
                    {
                        for (int j = a-1; j >= 0; j--)
                        {
                            try
                            {
                                MessageBox.Show(a.ToString());
                                ((DataGridViewComboBoxCell)dataGridView.Rows[e.RowIndex].Cells["deName"]).Items.RemoveAt(j);
                            }
                            catch(Exception e1)
                            {
                                MessageBox.Show(e1.Message);
                            }
                        }
                    }
      

  7.   

    各位,解决了。用clear()和remove()是可以的。是我程序另外地出问题了。