您的位置:首页 > 移动开发 > Objective-C

DataGridView里动态添加comboBox

2008-09-22 11:58 330 查看
      

  //DataGridView里动态添加comboBox

        //前提是有一个名为myDataGridView的DataGridView,第一列的属性为string,第二列的属性为string,第三列的属性为DataGridViewComboBoxCell

        object[] obj = { "0001", "性别", "0" };//"列1的内容", "列2的内容", "combox的初期值" 

        //新追加一行

        myDataGridView.Rows.Add(obj);

        //得到新追加行的第三个元素,前提是你的DataGridView的第三列的属性为comboBox

        comboBoxCell = (DataGridViewComboBoxCell)myDataGridView.Rows[0].Cells[2];

        comboBoxCell.Items.Clear();

        //向comboBox里追加内容

        comboBoxCell.Items.Add(new ComboData("0","男"));

        comboBoxCell.Items.Add(new ComboData("1", "女"));

        //设置comboBox与模型类的对应关系

        comboBoxCell.DisplayMember = "Display";

        comboBoxCell.ValueMember = "Value";

    //模型类

    public class ComboData

    {

        private string m_display = string.Empty;

        private string m_value = string.Empty;

        public ComboData(string display, string value)

        {

            this.m_display = display;

            this.m_value = value;

        }

        public string Display

        {

            get { return this.m_display; }

            set { this.m_display = value; }

        }

        public string Value

        {

            get { return this.m_value; }

            set { this.m_value = value; }

        }

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  string object